1 回答

TA貢獻1848條經驗 獲得超6個贊
使用clearInterval函數https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/clearInterval
WindowOrWorkerGlobalScope mixin 的 clearInterval() 方法取消了之前通過調用 setInterval() 建立的定時重復操作。
var interval = setInterval(function(){
var d = new Date()
var h = d.getHours()
var m = d.getMinutes()
var s = d.getSeconds()
h = (h < 10) ? ("0" + h) : h;
m = (m < 10) ? ("0" + m) : m;
s = (s < 10) ? ("0" + s) : s;
document.getElementById("time").value = h +":"+m+":"+s
}, 1000)
// this will cancel the interval when you want it to
clearInterval(interval);
添加回答
舉報