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

Timer StopWatch in JavaScript

P: n/a
Hi Guys

Hope this message shall find u all in gr8 moods.
As i was developing a Application for Online Exam and i am in need of a
way to maintain the time for the logged in user. So Can any one let me
know how can i do this with the help of JavaScript.

Regards

Lucky

Oct 1 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
wrote on 01 okt 2005 in comp.lang.javascript:
Hope this message shall find u all in gr8 moods.
As i was developing a Application for Online Exam and i am in need of a
way to maintain the time for the logged in user. So Can any one let me
know how can i do this with the help of JavaScript.


In general you cannot,
because the end of logging on is not well defined,
unless the logging off is done by the user.

If the concept is on one clientside scripted page:

start = new Date()

.......

end = new Date()

and read this NG's faq for substraction:
<http://www.merlyn.demon.co.uk/js-date1.htm#diff>

=================

If there are more pages a serverside solution is beter,
[in fact a serverside solution seems better anyway]

ASP-VBS example:

session("start")=now()

......

DurationSecs = DateDiff("s", now(), session("start") )


--
Evertjan.
The Netherlands.
(Replace all crosses with dots in my emailaddress)

Oct 1 '05 #2

P: n/a
lu*********@gmail.com wrote:
Hope this message shall find u all in gr8 moods.
As i was developing a Application for Online Exam and i am in need of a
way to maintain the time for the logged in user. So Can any one let me
know how can i do this with the help of JavaScript.


Hi Lucky.

Hers something to work with. If you need to time over multiple
pages , you'll need to store the value of dExamBegan in a cookie.
If you need that code too just ask.

<html>
<head>
<script type="text/javascript">
var dExamBegan=new Date()
function clock()
{
dNow=new Date()
c=document.getElementById("timecount")
iDuration=Math.floor((dNow-dExamBegan))
dDisplay = new Date(iDuration)
szMM=String(dDisplay.getMinutes());if(szMM.length< 2)szMM="0"+szMM
szSS=String(dDisplay.getSeconds());if(szSS.length< 2)szSS="0"+szSS
c.innerHTML=szMM+":"+szSS
setTimeout("clock()",1000)
}
</script>
</head>
<body onload="clock()"><div ID="timecount"
style="background-color:#aaaaaa;"></div></body>
</html>

--
--.
--=<> Dr. Clue (A.K.A. Ian A. Storms) <>=-- C++,HTML, CSS,Javascript
--=<> Internet Programming since 1994 <>=-- DHTML NSAPI TCP/IP
--=<> http://resume.drclue.net <>=-- AJAX, SOAP, XML, HTTP
--=<> http://www.drclue.net <>=-- SERVLETS,TCP/IP, SQL
--.
Oct 1 '05 #3

P: n/a
JRS: In article <46*****************@newsread1.news.pas.earthlink. net>,
dated Sat, 1 Oct 2005 19:05:36, seen in news:comp.lang.javascript, Dr
Clue <ia*********@mindspring.com> posted :
dDisplay = new Date(iDuration)
szMM=String(dDisplay.getMinutes());if(szMM.length <2)szMM="0"+szMM
szSS=String(dDisplay.getSeconds());if(szSS.length <2)szSS="0"+szSS
c.innerHTML=szMM+":"+szSS
That could more compactly be done by using something like
dDisplay.toString().match(/\d\d.\d\d /)
after checking possible toString results.

Otherwise, converting the time to seconds and using % and - and / to
convert will be brief enough and probably quicker.
setTimeout("clock()",1000)


That may not show the time once *every* second; easily fixed.

Note that UTC functions should be much faster then non-UTC.

--
John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 MIME.
Web <URL:http://www.merlyn.demon.co.uk/> - w. FAQish topics, links, acronyms
PAS EXE etc : <URL:http://www.merlyn.demon.co.uk/programs/> - see 00index.htm
Dates - miscdate.htm moredate.htm js-dates.htm pas-time.htm critdate.htm etc.
Oct 2 '05 #4

P: n/a
Dr John Stockton wrote:
JRS: In article <46*****************@newsread1.news.pas.earthlink. net>,
dated Sat, 1 Oct 2005 19:05:36, seen in news:comp.lang.javascript, Dr
Clue <ia*********@mindspring.com> posted :

dDisplay = new Date(iDuration)
szMM=String(dDisplay.getMinutes());if(szMM.lengt h<2)szMM="0"+szMM
szSS=String(dDisplay.getSeconds());if(szSS.lengt h<2)szSS="0"+szSS
c.innerHTML=szMM+":"+szSS

That could more compactly be done by using something like
dDisplay.toString().match(/\d\d.\d\d /)
after checking possible toString results.


I could have encrypted the example too :)

Not everyone is comfortable with regx, so unless
it is the topic, I'm probably not going to use a lot
of it in the examples unless it gives a really big
bang for it's cluttered syntax.
Otherwise, converting the time to seconds and using % and - and / to
convert will be brief enough and probably quicker.
setTimeout("clock()",1000)


That may not show the time once *every* second; easily fixed.


I think the OP was looking for something to time a test or quiz,
so the resolution / precision is well within reason for
an NG example provided for someone whos technical level is that
of one who needs help with a clock.

But you knew that;)

--
--.
--=<> Dr. Clue (A.K.A. Ian A. Storms) <>=-- C++,HTML, CSS,Javascript
--=<> Internet Programming since 1994 <>=-- DHTML NSAPI TCP/IP
--=<> http://resume.drclue.net <>=-- AJAX, SOAP, XML, HTTP
--=<> http://www.drclue.net <>=-- SERVLETS,TCP/IP, SQL
--.
Oct 3 '05 #5

P: n/a
JRS: In article <DT****************@newsread1.news.pas.earthlink.n et>,
dated Mon, 3 Oct 2005 18:37:23, seen in news:comp.lang.javascript, Dr
Clue <ia*********@mindspring.com> posted :
Dr John Stockton wrote:
JRS: In article <46*****************@newsread1.news.pas.earthlink. net>,
dated Sat, 1 Oct 2005 19:05:36, seen in news:comp.lang.javascript, Dr
Clue <ia*********@mindspring.com> posted :

dDisplay = new Date(iDuration)
szMM=String(dDisplay.getMinutes());if(szMM.leng th<2)szMM="0"+szMM
szSS=String(dDisplay.getSeconds());if(szSS.leng th<2)szSS="0"+szSS
c.innerHTML=szMM+":"+szSS

That could more compactly be done by using something like
dDisplay.toString().match(/\d\d.\d\d /)
after checking possible toString results.


I could have encrypted the example too :)

Not everyone is comfortable with regx, so unless
it is the topic, I'm probably not going to use a lot
of it in the examples unless it gives a really big
bang for it's cluttered syntax.


RegExps are so useful that they should be demonstrated whenever
reasonably possible, because too many readers don't take the trouble to
begin to use them and therefore produce long and cluttered code. That's
a pretty easy RegExp to start with, given that we know that it's
handling Time.
Otherwise, converting the time to seconds and using % and - and / to
convert will be brief enough and probably quicker.
setTimeout("clock()",1000)


That may not show the time once *every* second; easily fixed.


I think the OP was looking for something to time a test or quiz,
so the resolution / precision is well within reason for
an NG example provided for someone whos technical level is that
of one who needs help with a clock.


Such a one may be rather upset to discover an error of five percent in
some computers, and I expect a smaller error in others. By all means
show that code first; but indicate that it's not ideal.

<URL:http://www.merlyn.demon.co.uk/js-date0.htm#TaI>

--
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.
Oct 4 '05 #6

P: n/a


Dr John Stockton wrote:
Clue <ia*********@mindspring.com> posted :

Not everyone is comfortable with regx, so unless
it is the topic, I'm probably not going to use a lot
of it in the examples unless it gives a really big
bang for it's cluttered syntax.


RegExps are so useful that they should be demonstrated whenever
reasonably possible, because too many readers don't take the trouble to
begin to use them and therefore produce long and cluttered code. That's
a pretty easy RegExp to start with, given that we know that it's
handling Time.


Well, I think both our answers have their value, so
I think the answer is to have both answers available to the OP
and others that might fallow along over the years.

I appreciate the value and power of Regx and I do use it,
but IMHO it's powerful enough a topic to deserve it's own learning
space, and not be shortchanged in sharing the stage with a
discussion about a stopwatch script for someone who needs
help with such a thing as setTimeout.

If there is room enough for more concepts to be instilled
typically there will be more questions posted, and like bricks
in a wall , I'll answer their quesiotns and perhaps at one point or
another they will be the ones answering mine.

--
--.
--=<> Dr. Clue (A.K.A. Ian A. Storms) <>=-- C++,HTML, CSS,Javascript
--=<> Internet Programming since 1994 <>=-- DHTML NSAPI TCP/IP
--=<> http://resume.drclue.net <>=-- AJAX, SOAP, XML, HTTP
--=<> http://www.drclue.net <>=-- SERVLETS,TCP/IP, SQL
--.
Oct 4 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.