猿问

如何在Java Script中停止实时时间

我想在 java 脚本中停止直播时间。我该怎么做。这是我的实时java脚本源代码。


        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)



document.getElementById("time").setAttribute("disabled","disabled")

document.getElementById("time").clearInterval(value)


桃花长相依
浏览 109回答 1
1回答

慕勒3428872

使用clearInterval函数https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/clearIntervalWindowOrWorkerGlobalScope mixin 的 clearInterval() 方法取消了之前通过调用 setInterval() 建立的定时重复操作。var interval = setInterval(function(){&nbsp; &nbsp; &nbsp; &nbsp; var d = new Date()&nbsp; &nbsp; &nbsp; &nbsp; var h = d.getHours()&nbsp; &nbsp; &nbsp; &nbsp; var m = d.getMinutes()&nbsp; &nbsp; &nbsp; &nbsp; var s = d.getSeconds()&nbsp; &nbsp; &nbsp; &nbsp; h = (h < 10) ? ("0" + h) : h;&nbsp; &nbsp; &nbsp; &nbsp; m = (m < 10) ? ("0" + m) : m;&nbsp; &nbsp; &nbsp; &nbsp; s = (s < 10) ? ("0" + s) : s;&nbsp; &nbsp; &nbsp; &nbsp; document.getElementById("time").value = h +":"+m+":"+s}, 1000)// this will cancel the interval when you want it toclearInterval(interval);
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答