0

I've got a JS script that should change a html element - visibly count 1 to 100. When I add a timeout, the for loop doesn't loop anymore - jumps right to 100. It works with alert but not with the div change.

var theLabel = document.getElementById("counter");
 function doSetTimeout(i) {
  setTimeout(function() {
    /*alert(i); */
    theLabel.innerHTML = i;

    }, 1000);
  }

 for (var i = 1; i <= 100; i++)
 doSetTimeout(i);
0

1 Answer 1

0

http://jsfiddle.net/hqh6Lne9/

var i = 0;
theLabel = document.getElementById("counter");
var interval = setInterval(function(){ 
    if (i == 100) clearInterval(interval);
    theLabel.innerHTML = i; 
    i++;
}, 
1000);
Sign up to request clarification or add additional context in comments.

1 Comment

thanks, had no idea about the need to clear it

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.