Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 净距_Javascript_Angularjs_Ionic Framework_Setinterval - Fatal编程技术网

Javascript 净距

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

如何清除间隔,我尝试添加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) 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