Settimeout Inside A For Loop
Solution 1:
It's a common misconception that setTimeout
schedules events to run relative to previously queued events. It looks like you believe that, theoretically, the following:
setTimeout(f, 100);
setTimeout(g, 100);
setTimeout(h, 100);
would result in a timeline like this:
0ms Start
100ms Run f()
200ms Run g()
300ms Run h()
The reality is that the time option in setTimeout
means "run this function after at least this much time has passed." Going off of the previous example, you would actually get something more like
0ms Start
100ms Run f()
101ms Run g()
102ms Run h()
To space out your code correctly, keep adding to the timeout time rather than replacing it.
var time = 0;
for (var i = 1; i <= numberOfChilds; i++) {
currentIndexClass = '.alternateimage' + i;
currentImg = jQuery(currentIndexClass);
// Add to the previous time
time += parseInt(jQuery(currentIndexClass).attr("data-time"), 10);
changeImg(i, time, currentImg);
}
Solution 2:
Here is a fiddle implementing the use of timeout to achieve what you want.
.textArea {
position: absolute;
height: 50px;
width: 50px;
display: block;
}
.box_a {
background-color: blue;
}
.box_b {
background-color: red;
}
.box_c {
background-color: orange;
}
.active {
z-index: 3;
}
<div class="textArea box_a active">a</div>
<divclass="textArea box_b">b</div><divclass="textArea box_c">c</div>
$(function(){
var $elem = $('.textArea');
timeout(0);
functiontimeout(i){
$($elem[i]).addClass('active');
returnsetTimeout(function(){
$elem.removeClass('active');
i++;
if(i >= $elem.length){
i = 0
}
timeout(i);
}, 1000)
}
});
Note it does not use a for loop, because timeout is asynchronous and will not execute sequentially. Each timeout will fire at the same time basically, then do their action based on the wait time.
The solution is to make a function that keeps track of the index, and when the last timeout has completed execution.
Post a Comment for "Settimeout Inside A For Loop"