清除设定间隔

Clear set interval

 setInterval(function hello() {
  console.log('hello world');
  return hello;
}(), 6000);

如何清除间隔,我尝试添加变量间隔

var i =0;
var interval = setInterval(function hello() {
  console.log('world');
  if(++i == 3) clearInterval(interval);
  return hello;
}(), 5000);

但是没有用。感谢您的帮助。

clearInterval() 当然有效。您在以下代码行中有错误:

    }(), 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 

您应该将处理程序传递给 setInterval() 而不是就地调用它,