View Single Post
Old 11-11-2012, 11:05 PM   PM User | #5
bunzu
New Coder

 
Join Date: Jul 2012
Posts: 10
Thanks: 3
Thanked 0 Times in 0 Posts
bunzu is an unknown quantity at this point
Still doesn't seem to work



Quote:
Originally Posted by felgall View Post
this goes out of scope when the original object code has run and so when the setInterval runs it points to an entirely different object which doesn't have a durationproperty.

The usual way to keep this in scope after the object code has run is to set up another field (usually called self) that will continue to point to the current object in the subsequent code.

Try this:

Code:
var Object = {
    time: new Date().getTime, 
    duration: 2000, 
    self: this,
    test: setInterval(function(){console.log("hi hi");}, self.duration)
}

Object.test();
bunzu is offline   Reply With Quote