gpt4 book ai didi

javascript - 无法读取 setInterval 中的属性

转载 作者:行者123 更新时间:2023-12-02 15:45:54 26 4
gpt4 key购买 nike

在 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com