English 中文(简体)
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

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.



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! 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. That ID is then passed to the clearInterval function to stop what setInterval(function(){ sys.puts( interval ); }, 1000) is doing.

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.

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.





相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签