View Single Post
Old 11-11-2012, 09:46 PM   PM User | #4
felgall
Master Coder

 
felgall's Avatar
 
Join Date: Sep 2005
Location: Sydney, Australia
Posts: 5,530
Thanks: 0
Thanked 503 Times in 494 Posts
felgall is a jewel in the roughfelgall is a jewel in the roughfelgall is a jewel in the rough
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();
__________________
Stephen
Learn Modern JavaScript - http://javascriptexample.net/
Helping others to solve their computer problem at http://www.felgall.com/
felgall is offline   Reply With Quote