Re: Timer on webpages
Available news archives: comp.lang.tcl - comp.lang.python - comp.security.firewalls - sci.crypt - comp.lang.php - comp.lang.javascript
Google
 
Web news.hping.org


comp.lang.javascript archive

Re: Timer on webpages

From: Randy Webb <HikksNotAtHome@aol.com>
Date: Sat Apr 29 2006 - 20:34:28 CEST

Ben C said the following on 4/29/2006 11:53 AM:
> On 2006-04-29, David <david.goodyear@gmail.com> wrote:
>> Hi,
>>
>> Im not sure where to find all the documentation i need for this? I need
>> to timer since a start button has been pushed, and show a counter on a
>> page. If they click stop i want to keep the time, and carry on
>> incrementing it if they click start again.
>>
>> Any suggestions on code, or reference material for this?
>
> Tricky one.

Not really.

> I can't think of a way except using setInterval,

There are other ways but setInterval would be the best way to update the
page itself. Not necessarily the best way to increment the counter though.

> with an interval of about a second.

The resolution you used for setInterval would depend on how much
resource you would be willing to use and how accurate you wanted the
counter time wise.

> In the callback you update the counter, animation, etc.,
> and increment a variable that counts how many seconds have elapsed.

I don't agree with that one. Incrementing a variable would lead to
inaccuracies in the total. Just start with a Date object and compare the
current time, get the difference, and display it.

The only difference in this question and any other "running counter"
question is the Start/Stop buttons. And how you handled those would
depend, directly, on whether the OP wanted the timer to include the
Stopped time or not.

-- 
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Received on Mon May 1 05:26:57 2006