![]() | |
![]() |
| | Thread Tools | Display Modes |
#1
| |||
| |||
|
|
I am trying to make a page that starts with a number and displayes that number, then increments it once a second. The entire page is below - and this code locks up the browser. script var num_sold = 5000; function go() { while (num_sold > 0) { setTimeout("update()",1000); } } function update() { num_sold = (num_sold + 57); document.all.pens.innerHTML = num_sold; } /script |
#2
| |||
| |||
|
|
"Shawn Wilson" <shawnw (AT) dvigroup (DOT) net> kirjoitti viestissä:HYFQe.223599$uo4.115935 (AT) fe01 (DOT) news.easynews.com... I am trying to make a page that starts with a number and displayes that number, then increments it once a second. The entire page is below - and this code locks up the browser. script var num_sold = 5000; function go() { while (num_sold > 0) { setTimeout("update()",1000); } } function update() { num_sold = (num_sold + 57); document.all.pens.innerHTML = num_sold; } /script The proper way to do it to use setInterval(). With setInterval you define a schedule for a function, saying "evaluate expressions x every n seconds." In your case "evaluate update every seconds". The syntax goes like so: var loop = setInterval("update();", 1000); // 1000 means a thousand millisecods = 1 second // loop will be the timer ID Use that instead, and remove the go() function entirely, because that's the one locking your system up. If you want to stop the repetition at 5000 as it would seem to me, you could modify the function to clear the timer at 5000: function update() { num_sold = (num_sold + 57); if(num_sold>5000) // Test if 500 has been reached clearInterval(loop);// end the interval using the timer ID document.all.pens.innerHTML = num_sold; } -- SETI @ Home - Donate your cpu's idle time to science. Further reading at <http://setiweb.ssl.berkeley.edu/ Kimmo Laine <eternal.erectionN0 (AT) 5P4Mgmail (DOT) com |
#3
| |||
| |||
|
|
"Shawn Wilson" <shawnw (AT) dvigroup (DOT) net> kirjoitti viestissä:HYFQe.223599$uo4.115935 (AT) fe01 (DOT) news.easynews.com... I am trying to make a page that starts with a number and displayes that number, then increments it once a second. The entire page is below - and this code locks up the browser. script var num_sold = 5000; function go() { while (num_sold > 0) { setTimeout("update()",1000); } } function update() { num_sold = (num_sold + 57); document.all.pens.innerHTML = num_sold; } /script The proper way to do it to use setInterval(). With setInterval you define a schedule for a function, saying "evaluate expressions x every n seconds." In your case "evaluate update every seconds". The syntax goes like so: var loop = setInterval("update();", 1000); // 1000 means a thousand millisecods = 1 second // loop will be the timer ID Use that instead, and remove the go() function entirely, because that's the one locking your system up. If you want to stop the repetition at 5000 as it would seem to me, you could modify the function to clear the timer at 5000: function update() { num_sold = (num_sold + 57); if(num_sold>5000) // Test if 500 has been reached clearInterval(loop);// end the interval using the timer ID document.all.pens.innerHTML = num_sold; } -- SETI @ Home - Donate your cpu's idle time to science. Further reading at <http://setiweb.ssl.berkeley.edu/ Kimmo Laine <eternal.erectionN0 (AT) 5P4Mgmail (DOT) com |
![]() |
| Thread Tools | |
| Display Modes | |
| |