Is there any way to kill a setInterval loop through an Onclick button(有没有办法通过 Onclick 按钮杀死 setInterval 循环)
问题描述
因此,我使用附加到 onClick 的 setInterval 在此函数中进行了无限循环.问题是,我无法在 onClick 中使用 clearInterval 来阻止它.我认为这是因为当我将 clearInterval 附加到 onClick 时,它会杀死特定的时间间隔,而不是完全杀死函数.有什么办法可以通过 onClick杀死所有间隔?
So, I got an infinite loop to work in this function using setInterval attached to an onClick. Problem is, I can't stop it using clearInterval in an onClick. I think this is because when I attach a clearInterval to an onClick, it kills a specific interval and not the function altogether. Is there anything I can do to kill all intervals through an onClick?
这是我的 .js 文件和我的电话制作是
Here's my .js file and the calls I'm making are
input type="button" value="generate" onClick="generation();
input type="button" value="Infinite Loop!" onclick="setInterval('generation()',1000);"
input type="button" value="Reset" onclick="clearInterval(generation(),80;" // This one here is giving me trouble.
推荐答案
setInterval 返回一个句柄,你需要那个句柄才能清除它
setInterval returns a handle, you need that handle so you can clear it
最简单的,在你的 html 头中为句柄创建一个 var,然后在你的 onclick 中使用 var
easiest, create a var for the handle in your html head, then in your onclick use the var
// in the head
var intervalHandle = null;
// in the onclick to set
intervalHandle = setInterval(....
// in the onclick to clear
clearInterval(intervalHandle);
http://www.w3schools.com/jsref/met_win_clearinterval.asp
这篇关于有没有办法通过 Onclick 按钮杀死 setInterval 循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:有没有办法通过 Onclick 按钮杀死 setInterval 循环


- 失败的 Canvas 360 jquery 插件 2022-01-01
- Quasar 2+Apollo:错误:找不到ID为默认的Apollo客户端。如果您在组件设置之外,请使用ProvideApolloClient() 2022-01-01
- 使用RSelum从网站(报纸档案)中抓取多个网页 2022-09-06
- Fetch API 如何获取响应体? 2022-01-01
- Css:将嵌套元素定位在父元素边界之外一点 2022-09-07
- addEventListener 在 IE 11 中不起作用 2022-01-01
- Flexslider 箭头未正确显示 2022-01-01
- 如何使用 JSON 格式的 jQuery AJAX 从 .cfm 页面输出查 2022-01-01
- 400或500级别的HTTP响应 2022-01-01
- CSS媒体查询(最大高度)不起作用,但为什么? 2022-01-01