在 node.js 上我收到错误:
TypeError: Cannot read property 'talk_duration' of undefined
来自:setTimeout(this.hangup, this.data.talk_duration * 1000);
在 setInterval
.
但是,在 setInterval
之外我有console.log(this.data.talk_duration);
效果很好。
this.outcomeAnswer = function () {
console.log(this.data.talk_duration); //this work
num = 0;
db.run("INSERT INTO in_queue (action_id, state) VALUES ('" + this.data.action_id + "', 'InQueue')", function (error) {
queueCheckLoop = setInterval(function () {
num++;
if (num == 5) {
clearInterval(queueCheckLoop);
}
db.each("SELECT count(*) as total FROM agent_queue WHERE state = 'Ready'", function (err, row) {
if (row.total > 0) {
clearInterval(queueCheckLoop);
setTimeout(this.hangup, this.data.talk_duration * 1000);
}
});
}, 1000);
});
}
最佳答案
您需要记住 this
作为 oucomeAnswer 函数的第一行,例如:
var that=this;
然后使用
that.data.talk_duration
在具有不同作用域的函数中。
关于javascript - 无法读取 setInterval 中的属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32207924/