Reputation: 45
I have been trying to create a count up with JavaScript and it isn't working
I did:
function increment (){
for (var i = 0; i <= 100; i++){
text.innerHTML += parseInt(i)
}
}
setTimeout(increment, 200)
Upvotes: 0
Views: 476
Reputation: 8515
You need to use setInterval
in order to make an actual count up, if that's what you wanted to achieve:
function countUp() {
var i = 0; // a counter which is displayed every 100ms
// create interval which fires the callback every 100ms.
// `interval` holds the interval ID, which is later used to clear the
// interval (stop calling the callback)
var interval = setInterval(function() {
text.innerHTML = i++; // write `i` and increment
// if `i` is grater than 100 then clear the interval (stop calling the callback)
if (i > 100) clearInterval(interval);
}, 100);
}
countUp();
<div id="text"></div>
Upvotes: 4