简体   繁体   English

一个函数中的JavaScript setTimeout setInterval

[英]JavaScript setTimeout setInterval within one function

I think I might be overtired but I cannot for the life of me make sense of this, and I think it's due to a lack of knowledge of javascript 我想我可能会过度但我不能为我的生活理解这一点,我认为这是由于缺乏对javascript的了解

var itv=function(){
 return setInterval(function(){
  sys.puts('interval');
 }, 1000);
}
var tout=function(itv){
 return setTimeout(function(){
  sys.puts('timeout');
  clearInterval(itv);
 }, 5500);
}

With these two functions I can call 有了这两个功能,我可以打电话

a=tout(itv());

and get a looping timer to run for 5.5 seconds and then exit, essentially. 并获得一个循环计时器运行5.5秒,然后退出,基本上。



By my logic, this should work but it simply is not 根据我的逻辑,这应该有效,但事实并非如此

var dotime=function(){
 return setTimeout(function(){
  clearInterval(function(){
   return setInterval(function(){
    sys.puts("interval");
   }, 1000);
  });
 }, 5500);
}

any insight in this matter would be appreciated. 任何有关此事的见解将不胜感激。

it cannot work because because your setInterval will be called AFTER the timeout! 它无法工作,因为你的setInterval将在超时setInterval调用! your original approach is correct and you can still wrap this into single function: 您的原始方法是正确的,您仍然可以将其包装成单个函数:

var dotime=function(){
  var iv = setInterval(function(){
    sys.puts("interval");
  }, 1000);
  return setTimeout(function(){
    clearInterval(iv);
  }, 5500);
};

I think the mistake you're making is that the function itv doesn't return setInterval(function(){ sys.puts('interval'); }, 1000) it executes setInterval(function(){ sys.puts('interval'); }, 1000) and than returns back an ID that setInterval generates. 我认为你犯的错误是函数itv没有返回setInterval(function(){ sys.puts('interval'); }, 1000)它执行setInterval(function(){ sys.puts('interval'); }, 1000)并返回一个setInterval生成的ID。 That ID is then passed to the clearInterval function to stop what setInterval(function(){ sys.puts('interval'); }, 1000) is doing. 然后将该ID传递给clearInterval函数以停止setInterval(function(){ sys.puts('interval'); }, 1000)正在执行的操作。

Edit : An example of one function that would work. 编辑 :一个可以工作的函数的示例。

var dotime=function(){
 // Start our interval and save the id
 var intervalId = setInterval(function(){
  // This will get executed every interval
  sys.puts("interval");
 }, 1000);

 // Start our timout function
 setTimeout(function(){
  // This will get executed when the timeout happens
  clearInterval(intervalId); // Stop the interval from happening anymore
 }, 5500);
}

This is another way to write your version, you see that you pass a function to clearInterval, where you should have passed it a timer id. 这是编写你的版本的另一种方法,你看到你将一个函数传递给clearInterval,你应该在那里传递一个timer id。

var dotime=function(){
 var g=function(){
  var f=function(){
   return setInterval(function(){
    sys.puts("interval");
   }, 1000);
  }
  clearInterval(f);
 }
 return setTimeout(g, 5500);
}

To make it work you shoud call the function : 为了使它工作,你应该调用函数:

  clearInterval(f());

Or, using your version : 或者,使用您的版本:

var dotime=function(){
 return setTimeout(function(){
  clearInterval(function(){
   return setInterval(function(){
    sys.puts("interval");
   }, 1000);
  }());
 }, 5500);
}

Disclaimer : I didn't test this. 免责声明:我没有测试过这个。

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

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