0

Why this

for(x = 0; x < 10; x++){
  
  var delay = x;
  
  setTimeout(
    function(){
    	$('body').append(delay);
    }, delay)
  
}

Returns this: 9 9 9 9 9 9 9 9 9 9

instead of this : 0 1 2 3 4 5 6 7 8 9

http://jsfiddle.net/c1hotu9a/

3
  • The loop doesn't wait for the timeouts to trigger. Commented Dec 5, 2015 at 14:58
  • How can i get the second output? Commented Dec 5, 2015 at 14:59
  • I would not call it duplicate however your ans is here: stackoverflow.com/questions/3583724/… Commented Dec 5, 2015 at 15:04

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.