Diese Lösung verwendet Schließungen und Rekursion.
var SlideChanger = function(seconds_each) {
var index = -1;
// on the first cycle, index will be set to zero below
var maxindex = ($(".change_link").length) - 1;
// how many total slides are there (count the slide buttons)
var timer = function() {
// this is the function returned by SlideChanger
var logic = function() {
// this is an inner function which uses the
// enclosed values (index and maxindex) to cycle through the slides
if (index == maxindex)
index = 0; // reset to first slide
else
index++; // goto next slide, set index to zero on first cycle
$('.slideshow').blinds_change(index); // this is what changes the slide
setTimeout(logic, 1000 * seconds_each);
// schedule ourself to run in the future
}
logic(); // get the ball rolling
}
return timer; // give caller the function
}
SlideChanger(5)(); // get the function at five seconds per slide and run it
Was wir hier machen, die innere Funktion timer
außerhalb der Funktion aussetzt, in dem sie definiert wurde (SlideChanger
). Diese Funktion (timer
) hat Zugriff auf die Variablen, die in SlideChanger
(index
und maxindex
) definiert sind.
Jetzt, da wir die Variablen in der umgebenden Umgebung und eine Funktion für die Rückkehr zum Aufrufer eingerichtet haben, können wir die logische Engine in logic
einrichten. Wenn logic
ausgeführt wird, verwendet es index
und maxindex
, um zu bestimmen, welche Folie als nächstes angezeigt werden soll, zeigt die Folie und plant selbst, dass sie in Zukunft erneut ausgeführt wird.
Beim Aufruf ruft die zurückkehrende Funktion logic
auf, um den Ball ins Rollen zu bringen. Dann wiederholt sich logic
auf unbestimmte Zeit, indem es sich selbst so plant, dass es jedes Mal, wenn es ausgeführt wird, in Zukunft ausgeführt wird.
Also, um zusammenzufassen, rufen wir SlideChanger
mit einem numerischen Argument x
. Es gibt eine Funktion zurück, die nach dem Aufruf die Folie alle x
Sekunden ändert.
Eine andere Möglichkeit, das gleiche Konzept zu schreiben.
(function(seconds_each) {
var index = -1;
// on the first cycle, index will be set to zero below
var maxindex = ($(".change_link").length) - 1;
// how many total slides are there (count the slide buttons)
return function() {
// this is the function returned by SlideChanger
var logic = function() {
// this is an inner function which uses the
// enclosed values (index and maxindex) to cycle through the slides
if (index == maxindex)
index = 0; // reset to first slide
else
index++; // goto next slide, set index to zero on first cycle
$('.slideshow').blinds_change(index); // this is what changes the slide
setTimeout(logic, 1000 * seconds_each);
// schedule ourself to run in the future
}
logic(); // get the ball rolling
}
})(5)(); // get the function at five seconds per slide and run it
JavaScript ist eine schöne Sprache mit vielen funktionalen Programmierkonstrukte wie Funktionen höherer Ordnung (Funktionen, die entweder Funktionen erstellen oder Funktionen als Parameter übernehmen) und anonyme Funktionen. Weitere Informationen finden Sie unter http://www.ibm.com/developerworks/web/library/wa-javascript.html
Vielen Dank für die Veröffentlichung. Aber könntest du mir sagen, wo in der JS-Datei ich das einfügen soll? – catandmouse
Versuchen Sie es am unteren Rand von jquery.blinds-0.9.js – ecounysis
Funktioniert nicht. Ich habe den Codeblock kurz vor dem "}) (jQuery) eingefügt"; Am Ende. :? Habe ich es richtig gemacht? – catandmouse