Is there a way to make JavaScript print a counter continuously in the same place on the page?
for(var x=0; x<3000; x++){
document.write(x + '</br>');
document.body.innerHTML = "";
}
I want the number x to update and replace the previous number, however this code seems to wait until the loop is finished before showing a number (by then it is too late)
Is there a way to say, print number x, now wait 100ms, now clear and update in the same space the new value for x, and repeat?
(I know that the innerHTML
is killing the whole page, for now all this pages needs to do is run a counter, so other elements are irrelevant. What I don't want is a printed list of all those numbers, and I need a delay)
Thanks!
Is there a way to make JavaScript print a counter continuously in the same place on the page?
for(var x=0; x<3000; x++){
document.write(x + '</br>');
document.body.innerHTML = "";
}
I want the number x to update and replace the previous number, however this code seems to wait until the loop is finished before showing a number (by then it is too late)
Is there a way to say, print number x, now wait 100ms, now clear and update in the same space the new value for x, and repeat?
(I know that the innerHTML
is killing the whole page, for now all this pages needs to do is run a counter, so other elements are irrelevant. What I don't want is a printed list of all those numbers, and I need a delay)
Thanks!
Share Improve this question asked Aug 9, 2012 at 3:34 JosephJoseph 3,95710 gold badges34 silver badges53 bronze badges3 Answers
Reset to default 4The browser will not update the display while JavaScript is running. To make a timer happen you need to use setTimeout()
or setInterval()
. Also, avoid document.write()
unless you have a good reason to use it (if you're not sure what makes a good reason then don't use it at all).
You want something like this:
<body>
<div id="counter"></div>
</body>
Then:
window.onload = function() {
var x = 0,
max = 3000,
ctr = document.getElementById("counter");
function incrementCounter() {
ctr.innerHTML = x;
if (x++ < max)
setTimeout(incrementCounter, 100);
}
incrementCounter();
}
Demo: http://jsfiddle/nnnnnn/v5hmE/
Note that the above updates only the contents of the "counter" div, so any other elements on your page would not be affected.
Homework assignment for you: Google everything you didn't understand in that code.
Yes! For that purpose I would remend you two things:
To use a container (lets say a div) for that purpose:
and refer to it as
document.getElementById('counter').innerHTML = x
To use javascript timers before updating.
Hope that helps,
...however this code seems to wait until the loop is finished before showing a number...
Don't use a for
loop for this. You need a specific interval like you say. Use a function with a setTimeout
and some recursion:
var x = 0;
var speed = 100;
function update() {
if (x <= 3000) {
setTimeout(function() {
// update DOM here
x++;
update();
}, speed);
} else {
return false;
}
}
Demo: http://jsfiddle/elclanrs/PRC9R/
You can change the interval to say 9
so it looks believable but goes faster.
x += 9; // instead of x++