This in Timeouts

Setting a timeout on a class method that relies on another class method, causes errors as the callback function loses the reference to this. The solution: hand on the initial this as parameter to the callback.

{

empty: function() {
   return true;
},

process: function(self) {
   if(!self.empty()) {
       setTimeout( function() {
           self.process(self)},
           3000);
        }
    }
}

}
This entry was posted in Stream.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>