473,549 Members | 3,128 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 6188
"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("toD oWhen2SecsElaps ed();",2000);

function toDoWhen2SecsEl apsed(){
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 , "toDoWhen2SecsE lapsed();");
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.c om
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.getEle mentById(x);
if ( 'start' == a) {
startTime = new Date();
x.innerHTML = 'Delay in seconds: 0';
} else {
var t = new Date().getTime( ) - startTime.getTi me();
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************ ********@comcas t.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
correspondin g 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.demo n.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.c om/faq/> JL/RC: FAQ of news:comp.lang. javascript
<URL:http://www.merlyn.demo n.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demo n.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Jul 23 '05 #4
Dr John Stockton wrote:
JRS: In article <Br************ ********@comcas t.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
correspondi ng 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.demo n.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$236 56$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.ne t.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.c om/faq/> JL/RC: FAQ of news:comp.lang. javascript
<URL:http://www.merlyn.demo n.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demo n.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Jul 23 '05 #6
Dr John Stockton wrote:
JRS: In article <4274d71d$0$236 56$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.ne t.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******** ************@co mcast.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******** ************@co mcast.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
1992
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 stands supports syntax highlighting and outlining of functions, classes and their methods.] When I took over the project in June it was for the...
1
6016
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 window.all, forms, frames and such)? Some other way? When you create a timer (setTimeout), the return value is the timer id.
2
3865
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 http://home.pacific.net.sg/~jacksony ? (the drop down bar could not work at www.apchosting.net but can drop at home.pacific.net.sg. I suspect it is a server problem but was told it...
4
5655
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 that i cant disable this so i thought is it possible to do a check to see if a hidden text on the main form has a value. E.g. if we close the popup...
10
13446
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 the instance that is calling it (ie. notify.start('notify', 500)). Is there a way to have the instance read its own name and provide it as a string...
12
2055
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 me know what I need to change in the file to make a copy that would run fine on Firefox? Here is the link to the file:...
4
5215
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 javascript function which calls setTimeout and will process a second javascript function "Warn" just before the session expires. The Warn...
2
3275
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
4931
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: http://munich.schwarz-interactive.de/autocomplete.aspx
0
7520
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7956
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
7809
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5088
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3498
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3480
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1936
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1058
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
763
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.