Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript setInterval and `this` solution

I need to access this from my setInterval handler

prefs: null, startup : function()     {         // init prefs         ...         this.retrieve_rate();         this.intervalID = setInterval(this.retrieve_rate, this.INTERVAL);     },  retrieve_rate : function()     {         var ajax = null;         ajax = new XMLHttpRequest();         ajax.open('GET', 'http://xyz.com', true);         ajax.onload = function()         {             // access prefs here         }     } 

How can I access this.prefs in ajax.onload ?

like image 487
Pablo Avatar asked May 01 '10 08:05

Pablo


People also ask

How does the setInterval () function works in Javascript?

setInterval() The setInterval() method, offered on the Window and Worker interfaces, repeatedly calls a function or executes a code snippet, with a fixed time delay between each call. This method returns an interval ID which uniquely identifies the interval, so you can remove it later by calling clearInterval() .

What can I use instead of setInterval?

Nested setTimeout calls are a more flexible alternative to setInterval , allowing us to set the time between executions more precisely. Zero delay scheduling with setTimeout(func, 0) (the same as setTimeout(func) ) is used to schedule the call “as soon as possible, but after the current script is complete”.

Is it good to use setInterval in Javascript?

In order to understand why setInterval is evil we need to keep in mind a fact that javascript is essentially single threaded, meaning it will not perform more than one operation at a time.

Can we use two setInterval in Javascript?

Good question, but in JS you can't. To have multiple functions in the same program execute at the same time you need multi-threading and some deep timing and thread handling skills. JS is single threaded.


2 Answers

this.intervalID = setInterval(this.retrieve_rate.bind(this), this.INTERVAL); 
like image 116
Nechehin Avatar answered Sep 27 '22 16:09

Nechehin


The setInterval line should look like this:-

 this.intervalID = setInterval(      (function(self) {         //Self-executing func which takes 'this' as self          return function() {   //Return a function in the context of 'self'              self.retrieve_rate(); //Thing you wanted to run as non-window 'this'          }      })(this),      this.INTERVAL     //normal interval, 'this' scope not impacted here.  );  

Edit: The same principle applies to the " onload ". In this case its common for the "outer" code to do little, it just sets up the request an then sends it. In this case the extra overhead an additinal function as in the above code is unnecessary. Your retrieve_rate should look more like this:-

retrieve_rate : function() {     var self = this;     var ajax = new XMLHttpRequest();     ajax.open('GET', 'http://xyz.com', true);     ajax.onreadystatechanged= function()     {         if (ajax.readyState == 4 && ajax.status == 200)         {             // prefs available as self.prefs         }     }     ajax.send(null); } 
like image 20
AnthonyWJones Avatar answered Sep 27 '22 16:09

AnthonyWJones