inktherapy Posted November 4, 2008 Share Posted November 4, 2008 Hi,I am working on a timer, please see my code. My problem is if I click the stop, the time stops that works fine, but what I want is if I click the start button again I wanted it to start exactly where it stopped. Kindly teach me a work around for this. Thanks. <html><head><script type="text/javascript">function timeNow(){t = new Date;h = t.getHours();m = t.getMinutes();s = t.getSeconds();x=setTimeout('timeNow()',500);if (s<=9) {s = "0"+s;} if (h>12) {h = h - 12;pam = "pm";} else {pam = "am";} document.getElementById("display").innerHTML=h+" "+ m + " " + s + " " +pam;}function killTime() {clearTimeout(x);}</script><style type="text/css">#display {font-family:tahoma;font-weight:bold;font-size:12px;}</style></head><body><div id="display"></div><input type="button" value="Start" onclick="timeNow()" /><input type="button" value="Stop" onclick="killTime()" /></body></html> Link to comment Share on other sites More sharing options...
jeffman Posted November 4, 2008 Share Posted November 4, 2008 Try something like this:Create a global variable called timestamp. Set it to null.In your killTime() function, set the value of timestamp to the return value t.getTime().In timeNow(), if timestamp has a value, pass it to t.setTime(), else do nothing. The t object is now set to whatever time was stored in timestamp.To restore the counter to real time, reset timestamp to null.I may be forgetting something, so play around with this stuff. Link to comment Share on other sites More sharing options...
inktherapy Posted November 9, 2008 Author Share Posted November 9, 2008 Hey,Thank you I manage to made it working, I have a global var and another variable to hold on the timestamp as you advised. Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.