計時相關: 1.指定時間之后做一件事 setTimeout(js語句,毫秒) 2.每隔一段時間做一件事 setInterval(js語句,毫秒) clearInterval(setInterval的變量名)清除重復事件 <!DOCTYPE html> <html lang="zh-CN"> <head> <meta http-equiv="content-Type" charset="UTF-8"> <meta http-equiv="x-ua-compatible" content="IE=edge"> <title>Title</title> </head> <body> <input type="text" id="i1"> <button id="b1">開始</button> <button id="b2">停止</button> <script> var i1Ele = document.getElementById("i1"); var t; function f() { var now = new Date(); i1Ele.value = now.toLocaleString(); } f(); var b1Ele = document.getElementById("b1"); // 點開始 b1Ele.onclick = function (ev) { if (!t) { t = setInterval(f, 1000) } }; var b2Ele = document.getElementById("b2"); // 點停止 b2Ele.onclick = function (ev) { clearInterval(t); // 根據id清除定時任務 console.log(t); t = null; } </script> </body> </html>