Javascript 净距
如何清除间隔,我尝试添加var间隔Javascript 净距,javascript,angularjs,ionic-framework,setinterval,Javascript,Angularjs,Ionic Framework,Setinterval,如何清除间隔,我尝试添加var间隔 setInterval(function hello() { console.log('hello world'); return hello; }(), 6000); 但它不起作用。谢谢你的帮助。clearInterval()当然有效。以下代码行中有错误: var i =0; var interval = setInterval(function hello() { console.log('world'); if(++i == 3) c
setInterval(function hello() {
console.log('hello world');
return hello;
}(), 6000);
但它不起作用。谢谢你的帮助。clearInterval()当然有效。以下代码行中有错误:
var i =0;
var interval = setInterval(function hello() {
console.log('world');
if(++i == 3) clearInterval(interval);
return hello;
}(), 5000);
您应该将处理程序传递给
setInterval()
而不是就地调用它,clearInterval()
肯定可以工作。什么使你认为它不起作用?为什么你只想在每三次迭代中清除间隔?定义“…它不起作用”。你期待什么?到底发生了什么?您是否收到任何错误消息?代码在chrome中运行和停止……不确定问题出在哪里。@HawkenRives它以5秒的间隔显示“world”3次,然后退出。
}(), 5000);
//^^ remove () as this is not IIFE
var i =0;
var interval = setInterval(function hello() {
console.log('world');
if(++i == 3) clearInterval(interval);
return hello;
}(), 5000); // notice those parenthesis