Connecting Tech Pros Worldwide Forums | Help | Site Map

Need JavaScript Timer

Victor
Guest
 
Posts: n/a
#1: Jul 23 '05
I need a JavaScript timer - I have five events I need to time, that can be
triggered by a mouseclick event, or a keypress event. Each event is
separated by only one to two seconds.

The first event (mouse or key) starts the timer. Then, each time the event
(mouse or key) occurs, I need to display the present time in the
corresponding spot on the screen (so when it's over, the screen is
displaying five times for each corresponding event).

For anyone that can help, thanks!



Kimmo Laine
Guest
 
Posts: n/a
#2: Jul 23 '05

re: Need JavaScript Timer


"Victor" <VictorMoore1101@yahoo.com> kirjoitti
viestissä:BrydnRvsFJcYY-_fRVn-tA@comcast.com...[color=blue]
>I need a JavaScript timer - I have five events I need to time, that can be
> triggered by a mouseclick event, or a keypress event. Each event is
> separated by only one to two seconds.
>
> The first event (mouse or key) starts the timer. Then, each time the event
> (mouse or key) occurs, I need to display the present time in the
> corresponding spot on the screen (so when it's over, the screen is
> displaying five times for each corresponding event).
>
> For anyone that can help, thanks![/color]

just make it so that this line is executed when the event triggers
setTimeout("toDoWhen2SecsElapsed();",2000);

function toDoWhen2SecsElapsed(){
alert("It's been 2 seconds now");
// and put here everything you want to do when it'äs been done
}

Might be also
setTimeout(2000, "toDoWhen2SecsElapsed();");
I never remember in which order the parameters are... but I'm pretty sure
it's like I wrote it first.

I found pretty good example with google:
http://www.mcfedries.com/JavaScript/timer.asp
(When you get more experienced you can try Google yourself, but I did it for
you because it's pretty difficult for beginners. You can also ask us to use
Google for you in the future if you don't feel competent using it.)

--
"I am pro death penalty. That way people learn
their lesson for the next time." -- Britney Spears

eternal.erectionN0@5P4Mgmail.com


RobG
Guest
 
Posts: n/a
#3: Jul 23 '05

re: Need JavaScript Timer


Victor wrote:[color=blue]
> I need a JavaScript timer - I have five events I need to time, that can be
> triggered by a mouseclick event, or a keypress event. Each event is
> separated by only one to two seconds.
>
> The first event (mouse or key) starts the timer. Then, each time the event
> (mouse or key) occurs, I need to display the present time in the
> corresponding spot on the screen (so when it's over, the screen is
> displaying five times for each corresponding event).
>
> For anyone that can help, thanks!
>
>[/color]

You don't need a timer. Create a global variable and store the time
of the first event in it. On each subsequent event, get the time and
subtract it from the first time.

Some code below, you may want to spend a bit more time formatting the
output. I've got the output in seconds, but you may want something
else.

<script type="text/javascript">
var startTime;
function doTime(a, x){
var x = document.getElementById(x);
if ( 'start' == a) {
startTime = new Date();
x.innerHTML = 'Delay in seconds: 0';
} else {
var t = new Date().getTime() - startTime.getTime();
x.innerHTML += '<br>click: ' + t/1000;
}
}
</script>
<input type="button" value="Start..." onclick="
doTime('start','x');
">
<input type="button" value="Click me..." onclick="
doTime('blah','x');
">
<br><span id="x"></span>


--
Rob
Dr John Stockton
Guest
 
Posts: n/a
#4: Jul 23 '05

re: Need JavaScript Timer


JRS: In article <BrydnRvsFJcYY-_fRVn-tA@comcast.com>, dated Fri, 29 Apr
2005 23:41:49, seen in news:comp.lang.javascript, Victor
<VictorMoore1101@yahoo.com> posted :[color=blue]
>I need a JavaScript timer - I have five events I need to time, that can be
>triggered by a mouseclick event, or a keypress event. Each event is
>separated by only one to two seconds.
>
>The first event (mouse or key) starts the timer. Then, each time the event
>(mouse or key) occurs, I need to display the present time in the
>corresponding spot on the screen (so when it's over, the screen is
>displaying five times for each corresponding event).
>
>For anyone that can help, thanks![/color]

Kimmo's answered a different question.

RobG did not point out that, while the javascript date object itself has
a resolution of 1 ms by ECMA specification (but *may* be 1 s in Safari,
someone please check), the new Date() operation has an update
interval which depends on the O.S., and is likely to be 10 ms or worse,
maybe 55 ms.

Therefore, your resolution in timing one or two seconds may be about 5%
or 3%.

<URL:http://www.merlyn.demon.co.uk/js-dates.htm> reports update interval
resolution for the viewer's new Date().


If date objects are to be subtracted, .getTime() can be omitted.

In a general-purpose language on even an old PC, you can get access to
much better timing in the hardware; the CTC clock is 65536 times faster,
for example. See via below.

--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
<URL:http://www.jibbering.com/faq/> JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
RobG
Guest
 
Posts: n/a
#5: Jul 23 '05

re: Need JavaScript Timer


Dr John Stockton wrote:[color=blue]
> JRS: In article <BrydnRvsFJcYY-_fRVn-tA@comcast.com>, dated Fri, 29 Apr
> 2005 23:41:49, seen in news:comp.lang.javascript, Victor
> <VictorMoore1101@yahoo.com> posted :
>[color=green]
>>I need a JavaScript timer - I have five events I need to time, that can be
>>triggered by a mouseclick event, or a keypress event. Each event is
>>separated by only one to two seconds.
>>
>>The first event (mouse or key) starts the timer. Then, each time the event
>>(mouse or key) occurs, I need to display the present time in the
>>corresponding spot on the screen (so when it's over, the screen is
>>displaying five times for each corresponding event).
>>
>>For anyone that can help, thanks![/color]
>
>
> Kimmo's answered a different question.
>
> RobG did not point out that, while the javascript date object itself has
> a resolution of 1 ms by ECMA specification (but *may* be 1 s in Safari,
> someone please check),[/color]

Safari's date() resolution is 1 ms (Mac OS X 10.2.8 & Safari 1.0.3)
according to my testing and your page cited below.
[color=blue]
> the new Date() operation has an update
> interval which depends on the O.S., and is likely to be 10 ms or worse,
> maybe 55 ms.
>
> Therefore, your resolution in timing one or two seconds may be about 5%
> or 3%.[/color]

I presumed that the OP was timing user clicks and did not require a
high degree of precision. If that is required, there are many issues
involved in accurate measurement, though the resolution and update
interval of the date object are fairly fundamental. :-)

Also of interest would be system latency caused by being under load.
Event driven OS's tend to do things when they are ready, not exactly
when they are told. Any genuine test of user reaction times (if that
is what is being done) should not be built on top of JavaScript and
browser.
[color=blue]
>
> <URL:http://www.merlyn.demon.co.uk/js-dates.htm> reports update interval
> resolution for the viewer's new Date().
>
>
> If date objects are to be subtracted, .getTime() can be omitted.
>
> In a general-purpose language on even an old PC, you can get access to
> much better timing in the hardware; the CTC clock is 65536 times faster,
> for example. See via below.[/color]

If really accurate timing is required, best to go back to good 'ol
DOS or similar non-event driven operating system (and gives direct
access to system components, not via some other interface).


--
Rob
Dr John Stockton
Guest
 
Posts: n/a
#6: Jul 23 '05

re: Need JavaScript Timer


JRS: In article <4274d71d$0$23656$5a62ac22@per-qv1-newsreader-
01.iinet.net.au>, dated Sun, 1 May 2005 23:13:25, seen in
news:comp.lang.javascript, RobG <rgqld@iinet.net.auau> posted :
[color=blue][color=green]
>> RobG did not point out that, while the javascript date object itself has
>> a resolution of 1 ms by ECMA specification (but *may* be 1 s in Safari,
>> someone please check),[/color]
>
> Safari's date() resolution is 1 ms (Mac OS X 10.2.8 & Safari 1.0.3)
> according to my testing and your page cited below.
>[color=green]
>> the new Date() operation has an update
>> interval which depends on the O.S., and is likely to be 10 ms or worse,
>> maybe 55 ms.[/color][/color]

What is the update interval, which my page also gives?

Update interval is the average amount of change in new Date.getTime()
and numeric resolution is the HCF of those amounts; the latter cannot be
greater than the former, but may be less.

--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
<URL:http://www.jibbering.com/faq/> JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
RobG
Guest
 
Posts: n/a
#7: Jul 23 '05

re: Need JavaScript Timer


Dr John Stockton wrote:[color=blue]
> JRS: In article <4274d71d$0$23656$5a62ac22@per-qv1-newsreader-
> 01.iinet.net.au>, dated Sun, 1 May 2005 23:13:25, seen in
> news:comp.lang.javascript, RobG <rgqld@iinet.net.auau> posted :
>
>[color=green][color=darkred]
>>>RobG did not point out that, while the javascript date object itself has
>>>a resolution of 1 ms by ECMA specification (but *may* be 1 s in Safari,
>>>someone please check),[/color]
>>
>> Safari's date() resolution is 1 ms (Mac OS X 10.2.8 & Safari 1.0.3)
>> according to my testing and your page cited below.
>>
>>[color=darkred]
>>> the new Date() operation has an update
>>>interval which depends on the O.S., and is likely to be 10 ms or worse,
>>>maybe 55 ms.[/color][/color]
>
>
> What is the update interval, which my page also gives?
>
> Update interval is the average amount of change in new Date.getTime()
> and numeric resolution is the HCF of those amounts; the latter cannot be
> greater than the former, but may be less.
>[/color]


Safari 1.3 on OS X 10.3.9 ranges between 1 and 1.5 ms

Firefox on the same system ranges between 4 and 5 ms.


--
Rob
Victor
Guest
 
Posts: n/a
#8: Jul 23 '05

re: Need JavaScript Timer



"Victor" <VictorMoore1101@yahoo.com> wrote in message
news:BrydnRvsFJcYY-_fRVn-tA@comcast.com...[color=blue]
> I need a JavaScript timer - I have five events I need to time, that can be
> triggered by a mouseclick event, or a keypress event. Each event is
> separated by only one to two seconds.[/color]

Sorry, I should have written this clearer - the seperating events are AROUND
one to two seconds. So, it could be 1.25 sec, or 1.93 seconds, or maybe 0.73
seconds, etc.

Resolutions of 1/100 of a second is good enough - I'm limited by the
reaction time of the user, but I'm going to average them anyway.





RobG
Guest
 
Posts: n/a
#9: Jul 23 '05

re: Need JavaScript Timer


Victor wrote:[color=blue]
> "Victor" <VictorMoore1101@yahoo.com> wrote in message
> news:BrydnRvsFJcYY-_fRVn-tA@comcast.com...
>[color=green]
>>I need a JavaScript timer - I have five events I need to time, that can be
>>triggered by a mouseclick event, or a keypress event. Each event is
>>separated by only one to two seconds.[/color]
>
>
> Sorry, I should have written this clearer - the seperating events are AROUND
> one to two seconds. So, it could be 1.25 sec, or 1.93 seconds, or maybe 0.73
> seconds, etc.
>
> Resolutions of 1/100 of a second is good enough - I'm limited by the
> reaction time of the user, but I'm going to average them anyway.
>[/color]

You may will encounter issues where variance caused by the machine is
greater than that of your human subjects. Repeating tests with humans
has its own issues - humans learn, anticipate, get bored, distracted
etc. at different rates. Repetition does not necessarily guarantee
more accurate results. ;-)

Even the speed at which your machine displays images or plays sounds
that the subjects are responding to needs to be taken into account.

Many years ago I was involved in an exercise in testing responses.
The test machine ended up being an old DOS box with programs written
in assembler using a PS2 mouse. It was the only way to get direct
access to the system so that the appearance of images on the screen
and user clicks on a mouse could be accurately timed. Even the time
it took for the image to display needed to be taken into account.

With a USB mouse, even mouse clicks are delivered by a sub-system
that is prone to latency because of system load.

--
Rob
Closed Thread


Similar JavaScript / Ajax / DHTML bytes