By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,247 Members | 1,250 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,247 IT Pros & Developers. It's quick & easy.

Need JavaScript Timer

P: n/a
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!
Jul 23 '05 #1
Share this Question
Share on Google+
8 Replies


P: n/a
"Victor" <Vi*************@yahoo.com> kirjoitti
viestissä:Br********************@comcast.com...
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!


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

et****************@5P4Mgmail.com
Jul 23 '05 #2

P: n/a
Victor wrote:
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!


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
Jul 23 '05 #3

P: n/a
JRS: In article <Br********************@comcast.com>, dated Fri, 29 Apr
2005 23:41:49, seen in news:comp.lang.javascript, Victor
<Vi*************@yahoo.com> posted :
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'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.
Jul 23 '05 #4

P: n/a
Dr John Stockton wrote:
JRS: In article <Br********************@comcast.com>, dated Fri, 29 Apr
2005 23:41:49, seen in news:comp.lang.javascript, Victor
<Vi*************@yahoo.com> posted :
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'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),


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.
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%.
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.

<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.


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
Jul 23 '05 #5

P: n/a
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 <rg***@iinet.net.auau> posted :
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),


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.
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.


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.
Jul 23 '05 #6

P: n/a
Dr John Stockton wrote:
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 <rg***@iinet.net.auau> posted :

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),


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.

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.

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.

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
Jul 23 '05 #7

P: n/a

"Victor" <Vi*************@yahoo.com> wrote in message
news:Br********************@comcast.com...
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.


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.

Jul 23 '05 #8

P: n/a
Victor wrote:
"Victor" <Vi*************@yahoo.com> wrote in message
news:Br********************@comcast.com...
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.

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.


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
Jul 23 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.