Id returned by setInterval()?

Hi all,
a simple question for which I’m a bit cofused…
What is the meaning of the id returned by setInterval(), is it refering to the number of intervals instantiated in a variable, and if so is there a way to clear them all at once?
Many thanks for your answers,
Andres

The setInterval() returns the instance ID of that interval running in memory, this ID can be used to clear that interval with clearInterval(<id>) function.

For ex. the code below sets up two intervals and then clears the 1st interval.



var id1 = window.setInterval('some_function', 100);
var id2 = window.setInterval('some_other_function', 500);

window.clearInterval(id1);


I see thanks for the explanation…
I think my only question now is if the window. code has to be used on every scope or could I, inside a prototype, say something like…


ImageSlider= function(options){
 this.interval=undefined;	
};
ImageSlider.prototype={	
  start:function(){
    var self=this;
    this.interval=setInterval(function(){self.some_function()}, 100);
  },
  stop:function(){
  clearInterval(this.Interval);
  }
}

Is the code above potentially not good enough so I end up firing lots of events in other functions?

I think you have a good start, just a couple of slight improvements.

ImageSlider= function(options){
 this.interval=undefined;	
};
ImageSlider.prototype={	
  start:function(){
    var self=this;
    if (this.interval == undefined)
       this.interval=setInterval(function(){self.some_function()}, 100);
  },
  stop:function(){
    if (this.interval != undefined)
    {
       clearInterval(this.interval);
       this.interval = undefined;
    } 
  }
}

This way you can’t create an orphaned interval if you were to call imageSlider.start(); then call imageSlider.start() again before calling imageSlider.stop(). Likewise, you can’t try to clear an undefined interval by calling imageSlider.stop() before you ever called imageSlider.start().

Lastly, in the stop(), I sent the this.interval back to undefined, because I believe it keeps the instance number even after you call clearInterval, so this now permits you to do imageSlider.start(); imageSlider.stop(); imageSlider.start();

Perfectly clear now,
thanks for taking the time…