如何解决'TypeError:对象#<Object>的属性'_TimeTimeout'不是节点中的函数?

Raj*_*Raj 4 javascript node.js

以下剪辑代码:

var theAdNets = new Array();

function populateAdNetList() {


}

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

产生以下错误:

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

populateAdNetList()是一个函数,而不是一个对象.在函数(){}的主体中没有引用'this'.为什么会发生这种情况?

Guf*_*ffa 5

这很可能是范围问题.如果函数是在函数或对象中本地定义的,那么它将在将要计算字符串的全局范围内不可用.

将函数移动到全局范围,或者在调用中使用函数引用来避免范围问题:

//update the list every 5 minutes.
setInterval(populateAdNetList,300000);
//fetch it in 5 seconds.
setTimeout(populateAdNetList,5000);
Run Code Online (Sandbox Code Playgroud)