HighDots Forums  

Re: Update screen counter live continuously

JavaScript discussion (multi-lingual) JavaScript discussion (alt.comp.lang.javascript)


Discuss Re: Update screen counter live continuously in the JavaScript discussion (multi-lingual) forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Kimmo Laine
 
Posts: n/a

Default Re: Update screen counter live continuously - 08-29-2005 , 11:26 AM






"Shawn Wilson" <shawnw (AT) dvigroup (DOT) net> kirjoitti
viestissä:HYFQe.223599$uo4.115935 (AT) fe01 (DOT) news.easynews.com...
Quote:
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>




Reply With Quote
  #2  
Old   
Shawn Wilson
 
Posts: n/a

Default Re: Update screen counter live continuously - 08-29-2005 , 11:39 AM






Thanks.

I don't want to stop at 5000 though, that's the input number that gets
printed on the screen, then incremented and re-printed. So it should go
5000, 5057, etc... In reality I set this with a PHP call because I pull
that number from a DB, but that's beside the point.

I'll try what you suggested.

---------------
Shawn Wilson



"Kimmo Laine" <eternal.erectionN0.5P (AT) Mgmail (DOT) com> wrote

Quote:
"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




Reply With Quote
  #3  
Old   
Shawn Wilson
 
Posts: n/a

Default Re: Update screen counter live continuously - 08-29-2005 , 11:44 AM



That worked perfectly...

One last thing... how do I format a number with commas? Nothing fancy, just
thousands seperators so 1000000000 becomes 1,000,000,000

Thanks!

---------
Shawn Wilson


"Kimmo Laine" <eternal.erectionN0.5P (AT) Mgmail (DOT) com> wrote

Quote:
"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




Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.