admin管理员组文章数量:1395274
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++
本文标签: timerUpdate number in same place on web page with javascriptStack Overflow
版权声明:本文标题:timer - Update number in same place on web page with javascript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744718008a2621523.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论