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);
yadayada
  • 3
  • 1
  • 5

1 Answers1

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);
waki
  • 1,248
  • 2
  • 17
  • 27