You are on page 1of 1

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.

org/1999/xhtml" xml:lang="en-us"> <head> <title>Timer</title> <script type="text/javascript"> var timeInput; var timeButton; var timeDiv; var timerDate; var timerHandler; function startTimer() { var timeStr = timeInput.value; if(timeStr == null || timeStr == "") return; var timeValues = timeStr.split(":"); var th = parseInt(timeValues[0], 10); var tm = parseInt(timeValues[1], 10); var ts = parseInt(timeValues[2], 10); timerDate = new Date(); timerDate.setHours(th); timerDate.setMinutes(tm); timerDate.setSeconds(ts); if(timerHandler) { timerHandler = clearInterval(timerHandler); } if(th == 0 && tm == 0 && ts == 0) return; if(timeButton.value == "Start") { timerHandler = setInterval(update, 1000); timeButton.value = "Stop"; } else { timeButton.value = "Start"; } } function update() { var th = timerDate.getHours(); var tm = timerDate.getMinutes(); var ts = timerDate.getSeconds(); var str = (th < 10 ? ( "0" + th ): th) + ":" + (tm < 10 ? ( "0" + tm ): tm) + ":" + (ts < 10 ? ( "0" + ts ): ts); timeDiv.innerHTML = str; timerDate.setSeconds(--ts); } </script> </head> <body> <input type="text" id="time"></input> <input type="button" id="start" value="Start" onclick="startTimer();"></inpu t> <div id="apDiv3"></div> <script type="text/javascript"> timeInput = document.getElementById('time'); timeDiv = document.getElementById('apDiv3'); timeButton = document.getElementById('start'); </script> </body></html>

You might also like