javascript - JS: How to reset index inside a for-loop so it loops again without lots of timeout errors? -
i'm working on custom js thumbnail changer , works. want loop thumbnails indefinitely. @ moment goes until i==thumbs
, stops. here codesnipped:
for (var = 1; <= thumbs; i++) { timer[i] = settimeout("thumbchange('" + image_id + url + + '.jpg' + "')", * 100 * 10); }
what want is, if i
reaches thumbs
(the last thumbnail), reset i
1 , play for
loop again. can't work.
i tried lots of things including following, crashes browser because of timeouts: can somehow around timeout problem completely?
for (var = 1; <= thumbs; i++) { timer[i] = settimeout("thumbchange('" + image_id + url + + '.jpg' + "')", * 100 * 10); if (i == thumbs) { = 1; cleartimeout(timer[i]); continue; } }
alternatively use setinterval
automatically loop forever or until clear it. note in setinterval
, settimeout
should use functions not evaluation strings.
var img = 0; thumbchange (image_id + url + img + '.jpg'); // initialise img 0 window.setinterval (function () { // invoked once per interval if (++img == thumbs) // inc image , reset 0 when reaches thumbs img = 0; thumbchange (image_id + url + img + '.jpg'); // image change }, 1000); // 1 second per image
Comments
Post a Comment