I have a JavaScript object with a privileged method. When this method has completed, I would like it to call itself (after a small timeout) and continue running indefinitely
because the this
in your setTimeout is referring to the local function testMethod
not Test
-- essentially, you are saying setTimeout( testMethod.testMethod, 2000 )
function Test() {
// ... private variables that testMethod needs to access ...
var self = this;
self.testMethod = function() {
alert("Hello, from the method.");
setTimeout(self.testMethod, 2000);
};
}
var myTest = new Test();
myTest.testMethod();