I am new to js and I am trying tutorials for a countdown clock with buttons to activate the functions. I have gotten it to start and countdown to 0 then alert a message and I got a clear button that clears it to 0 and a stop button to stop at whatever time before 0. My problem is I have not been able to get a button to work to resume the countdown at the time stopped. Was wondering if anyone can help out? Here is the code I have for the 3 buttons. How can I do a function for the time it is stopped at?
Code:var running = false var endTime = null var timerID = null function startTimer() { running = true now = new Date() now = now.getTime() // change last multiple for the number of minutes endTime = now + (1000 * 30 * 1) showCountDown() } function showCountDown() { var now = new Date() now = now.getTime() if (endTime - now <= 0) { stopTimer() alert("Time is up. Get back to work!.") } else { var delta = new Date(endTime - now) var theMin = delta.getMinutes() var theSec = delta.getSeconds() var theTime = theMin theTime += ((theSec < 10) ? ":0" : ":") + theSec document.forms[0].timerDisplay.value = theTime if (running) { timerID = setTimeout("showCountDown()",1000) } } } function stopTimer() { clearTimeout(timerID) running = false document.forms[0].timerDisplay.value = "0:00" } function stopTimertwo() { clearTimeout(timerID) running = false document.forms[0].timerDisplay.value = theTime }Code:<html> <head> <title>Countdown clock</title> <script type = "text/javascript" src="clock.js"></script> </head> <body> <FORM> <INPUT TYPE="button" NAME="startTime" VALUE="Start/Reset" onClick="startTimer()"> <INPUT TYPE="button" NAME="stopTime" VALUE="Stop Timer" onClick="stopTimertwo()"> <INPUT TYPE="button" NAME="clearTime" VALUE="Clear Timer" onClick="stopTimer()"> <INPUT TYPE="text" NAME="timerDisplay" VALUE="0:30"> </FORM> </body> </html>



Reply With Quote


Bookmarks