Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

A Timer using cookies 1

Status
Not open for further replies.

neillovell

Programmer
Aug 27, 2002
560
GB
Hi all,
I have a javascript timer in my webpage at the moment, but users are complaining that when they close the browser window the timer stops running.
"So don't close it" I say but they didn't like that much.

So...how do I write a cookie that stores the time the START button is pressed, then calculates the difference between that and the current time when STOP is pressed?
 
i'm not sure if i understood what your post, but this is my guess at it
Code:
<html>
<head>
<Script language='javascript'><!--
function startTimer()
{
var starttime = new Date();
var time1 = starttime.getTime();
document.cookie = &quot;time1 = &quot; + time1;
}
//********this next part could be on another page*********\function stopTimer()
{
var stoptime = new Date();
var cookie = document.cookie;
if (cookie.indexOf(';') != -1)
	{
	var cookie2 = document.cookie.split(';');
	var cookie3 = cookie2[0];
	var cookie4 = cookie3.split('=');
	var firsttime = cookie4[1];
	}
else
	{
	var cookie4 = cookie.split('=');
	var firsttime = cookie4[1];
	}
var time2 = stoptime.getTime();
var diff = (time2 - firsttime) * 0.001;
alert (diff);
}
//end of the part that could be on another page
//--></script>
</head>
<body>
<center>
Timer...thinger
<form name='form1'>
<input type=button value='Start Timer' onClick='startTimer()'>
<input type=hidden name=time>
<input type=button value='Stop Timer' onClick='stopTimer()' name=stop>
</form>
</center>
</body>
</html>

hope it helps &quot;Those who dare to fail miserably can achieve greatly.&quot; - Robert F. Kennedy
So true..
 
I've given you a star because I've learnt from your code, but it's not exactly what I'm after (I now know that a cookie perhaps is not what I need).

I want the user to be able to hit START, then go off and do whatever they like (close the browser, restart the computer, etc.) and be able to come back and hit STOP to see how much time has elapsed.

Do cookies get destroyed after the window closes / PC gets rebooted? I'm sure on some websites (like amazon, ebay, pets.com) they keep you logged in.
 
&quot;So don't close it&quot; I say but they didn't like that much.
:)
Perfect example of one of the many things I'd like to say to users.
:)

User: It breaks when I do this...
Programmer: Then don't do that!

Is that like:
User: I accidentally folded my child in the stroller because it iddn't tell me I couldn't
Judge: Ok, you win your lawsuit, from now on they will place a little label on strollers to keep people from closing them with their children in them

-Tarwn --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- ---
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top