Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to resolve 'TypeError: Property '_onTimeout' of object #<Object> is not a function' in node?

The following snipped of code:

var theAdNets = new Array();

function populateAdNetList() {


}

//update the list every 5 minutes.
setInterval("populateAdNetList()",300000);
//fetch it in 5 seconds.
setTimeout("populateAdNetList();",5000);

Produces the following error:

TypeError: Property '_onTimeout' of object #<Object> is not a function
    at Timer.callback (timers.js:83:39)

The populateAdNetList() is a function and not an object. There is no reference to 'this' in the body of the function() { }. Why could this be happening?

like image 274
Raj Avatar asked Dec 22 '22 11:12

Raj


2 Answers

That is most likely a scope issue. If the function is defined locally in a function or object, then it's not available in the global scope where the string will be evaluated.

Move the function to the global scope, or use the function reference in the calls instead to avoid the scope problem:

//update the list every 5 minutes.
setInterval(populateAdNetList,300000);
//fetch it in 5 seconds.
setTimeout(populateAdNetList,5000);
like image 151
Guffa Avatar answered Dec 24 '22 00:12

Guffa


You have to pass function to setInterval. Not string and not the result of the function (until it is also a function). The proper syntax:

setInterval(populateAdNetList, 300000);
setTimeout(populateAdNetList, 5000);
like image 45
Igor Dymov Avatar answered Dec 24 '22 02:12

Igor Dymov