SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Zealot
    Join Date
    May 2008
    Posts
    162
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    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

  2. #2
    Founder of Primal Skill Ltd. feketegy's Avatar
    Join Date
    Aug 2006
    Posts
    482
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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.

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

  3. #3
    SitePoint Zealot
    Join Date
    May 2008
    Posts
    162
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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...
    Code:
    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?

  4. #4
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,219
    Mentioned
    153 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Andres Vaquero View Post
    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...
    Code:
    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.
    Code:
    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();

  5. #5
    SitePoint Zealot
    Join Date
    May 2008
    Posts
    162
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Perfectly clear now,
    thanks for taking the time...


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •