473,405 Members | 2,379 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

Need JavaScript Timer

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
8 6175
"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
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
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
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
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
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

"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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Alex Fitzpatrick | last post by:
Just by way of introduction, I'm currently the principal developer and maintainer of the a JavaScript editor plug-in for Eclipse. https://sourceforge.net/projects/jseditor/ The plug-in as it...
1
by: Antoine | last post by:
Hello, Does anybody know a way to retreive the running timers (their ids) in a html page? I cannot find something in the html dom at first sight. Is there collection of timers available (like...
2
by: Jackson Yap | last post by:
can someone kind enough to help me look at the attached html and js file? Why is it that the javascript menu could not work at www.apchosting.net but could work at...
4
by: Colin Graham | last post by:
Hi guys, Just a quickie here that i hope someone can help me with. Basically i want stop the user from closing the popup window using the small x button in the top right hand corner. Im aware...
10
by: Chris Lieb | last post by:
Hi, I am working on an object that has a function that calls setInterval so that it can have a function called repeatedly. Right now, to call the function, you also have to provide the name of...
12
by: shafiqrao | last post by:
Hello everyone, I have a script that runs in IE great, but in firefox it has problems. I understand that there are some objects that are accessed differently in IE and Mozilla. Can anybody let...
4
by: E | last post by:
I am having trouble with setTimeout working on a second call to the setTimeout function from a second page which is an html page. Here is the scenario. I have a web page and onload it calls a...
2
by: alxasa | last post by:
Hi, I have a setInterval which executes its command every 10 seconds in a infinite loop. I've got something real basic like: var processes=0; function startme(){ if(stopthisloop>1)
1
by: Bob | last post by:
Hi, Hope you can help me with this one. I'm at my wits end. I'm trying to create an intelligent edit-box like the excellent "Customer" one at the URL: ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.