1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
| <!DOCTYPE html> <html>
<head> <meta charset="utf-8"> <title>JS Bin</title> <style> .wrapper { display: flex; justify-content: center; }
#outputTime { margin: 0 atuo; text-align: center; } </style> </head>
<body> <div class="wrapper"> <select placeholder="请选择时间" id="Countdown"> <option value="1">1分钟</option> <option value="5">5分钟</option> <option value="10">10分钟</option> <option value="60">60分钟</option> <option value="1000">1000分钟</option> </select> <button id="startButton">start</button> <button id="pauseButton">pause</button> <button id="resumeButton">resume</button> </div>
<div id="outputTime"> </div> <script> let timeLeft let lastTiemId
startButton.onclick = function() { pauseButton.disabled = false var timeValue = parseInt(Countdown.value, 10) timeLeft = timeValue * 60 if (lastTiemId) { window.clearTimeout(lastTiemId) } showTime() resumeButton.disabled = true }
function CountTime() { var hours = Math.floor(timeLeft / (60 * 60)) var minutes = Math.floor(timeLeft / 60) - hours * 60 var seconds = timeLeft % 60 var s = seconds < 10 ? "0" + seconds : seconds var m = minutes < 10 ? "0" + minutes : minutes var h = hours < 10 ? "0" + hours : hours showValue = h + ":" + m + ":" + s }
function showTime() { CountTime(timeLeft) outputTime.textContent = showValue if (timeLeft === 0) { return } timeLeft -= 1 lastTiemId = setTimeout(showTime, 1000, timeLeft) }
pauseButton.onclick = function() { if (lastTiemId) { window.clearTimeout(lastTiemId) } pauseButton.disabled = true resumeButton.disabled = false } resumeButton.onclick = function() { pauseButton.disabled = false resumeButton.disabled = true showTime() } </script> </body>
</html>
|