Two "for" loops, one after the other

I'd like these two "for" loops to be executed one after the other (i.e. in this order).
The second loop has a different id (y)... Shouldn't it work?


	setTimeout(function(){
	//////////////////////
for (var i=0; i < 100; i++) {
	setTimeout(i, function(){
		// do first thing
	}, i * 5)
}

setTimeout(function(){
//////////////////////
for (var y=0; y < 444; y++) {
	setTimeout(y, function(){
		// do second thing
	}, y * 2)
}

},i+100) // second loop is delayed  i + 100 ms
},10) // first loop is delayed 10 ms

You missed the second reason exposed in my answer to your previous post : [widget script property] "for" loop delay - #2 by jean-emmanuel

Edit: wait, ok the inner setTimeout do have ids sorry. You still need to add the total time of loop i to loop y's timeout otherwise loops i and y will run concurrently.

1 Like

So the second loop shouldn't be delayed i + 100 ms.
It should be delayed i * 5 + 100 ms.
I thought i gets a new value with each iteration. My bad. I hate javascript already. :stuck_out_tongue:

Corrected code
// 1st FOR loop (i)
for (var i=0; i < 100; i++) {
	setTimeout(i, function(){
		// do first thing
	}, i * 5)
}

// 2nd FOR loop (y)
setTimeout(function(){
//////////////////////
for (var y=0; y < 444; y++) {
	setTimeout(y, function(){
		// do second thing
	}, y * 2)
}

},i * 5 + 100)