简体   繁体   English

如何使用清除按钮“清除”我的计时器?

[英]How do I "clear" my timer using my clear button?

I am new to javascript.我是 javascript 新手。 I am trying to clear my timer value on clcking the"clear" button.I tried to use the variable"sec" from myTimer function, but it is not working.我试图在点击“清除”按钮时清除我的计时器值。我尝试使用 myTimer 函数中的变量“秒”,但它不起作用。 Any suggestions?有什么建议? Help is appreciated.帮助表示赞赏。

let count = 0
let myTimer;

// set timer
myTimer = setInterval(() => count = count + 1 , 1000)

// clear timer
clearInterval(myTimer)

Show the code of your timer so that I can describe the solution in more detail显示您的计时器的代码,以便我可以更详细地描述解决方案

let timer;
let increment = 0;

timer = setInterval(() => increment++ , 1000)

//gets the button element and clears timer when clicked
document.getElementsByTagName("button").onclick = function() {
    clearInterval(timer)
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM