Jump to content

A simple JS question: clearInterval()?


tinfanide

Recommended Posts

var timer;function func(){timer = setInterval(function(){  alert("message..."); // Any difference from without clearInterval(timer)???  clearInterval(timer);//  func();  },5000);}window.onload = func;

See the comment above, please.I've tested it. It seems no difference at all.But to the best I know, a setInterval without a clearInterval will eventually run after the previous interval, thus producing endless running of the codes and a crash, eh? Thanks in advance.

Link to comment
Share on other sites

setInterval() will run a function repeatedly upon each time interval. clearInterval() stops the repetition. If you only want to run the function once you should use setTimeout() instead. The alert() in your code will interfere with the timing. If you're using setInterval then you won't need to place it inside the function that's being called.

Link to comment
Share on other sites

setInterval() will run a function repeatedly upon each time interval. clearInterval() stops the repetition. If you only want to run the function once you should use setTimeout() instead. The alert() in your code will interfere with the timing. If you're using setInterval then you won't need to place it inside the function that's being called.
What do you mean by the alert() interfering with the timing?I want the alert() to run every 5 seconds, so I didn't opt for setTimeOut.And can you tell me more about not necessary to place setInterval inside the function?
Link to comment
Share on other sites

The timer will be stopped until the person clicks "OK" on the alert box. setInterval() will keep on executing the function. Therefore the function does not need to call itself.

var timer;function func(){    timer = setInterval(function() { alert("message..."); }, 5000);}window.onload = func;

Here's the same example with setTimeout()

var timer;function func(){    timer = setTimeout(function() { alert("message..."); func(); }, 5000);}window.onload = func;

setTimeout() only executes the function once, so you have to call the function again once the timer finished. For repetitive tasks there's no need for clearTimeout() or clearInterval().

Link to comment
Share on other sites

The timer will be stopped until the person clicks "OK" on the alert box. setInterval() will keep on executing the function. Therefore the function does not need to call itself.
var timer;function func(){	timer = setInterval(function() { alert("message..."); }, 5000);}window.onload = func;

Here's the same example with setTimeout()

var timer;function func(){	timer = setTimeout(function() { alert("message..."); func(); }, 5000);}window.onload = func;

setTimeout() only executes the function once, so you have to call the function again once the timer finished. For repetitive tasks there's no need for clearTimeout() or clearInterval().

Yes, it's clear now.clearInterval() in my example is more than needed.func() again calls it.But two of them are not needed in my first examplebecause it's repeated tasks.
Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...