所以基本上我有一个firebase cloud onWrite函数,该函数在执行时会调用客户端以提醒他打开仪表板。我正在使用nexmo api。无论如何,我正在尝试使函数在执行之前等待一段时间,因为值可能会更改,如果它更改(不调用客户端),如果没有更改请调用他
像这样:-
// When new order is placed call the function
// Wait for 10 min if there is any change
(if there is no change to that value) {
call();
} else {
if the value changed during that time stop the function
return;
}
我已经尝试过setTimeout(),但是它会根据初始值执行,我希望它在10分钟后根据最终值执行。
最佳答案
我做到了,我在客户端创建了一个计数器,并在10分钟后使用setTimeout()将其重置为0,如果计数器为零且值仍然相同,则调用client();。
这样,由于计数器不为0,因此第一次将其添加到数据库时不会进行调用。
在客户端,我做到了
// update counter to 0 after 15 seconds
setTimeout(() => {
firebase
.database()
.ref('the place where you want to update')
.update({ counter: 0 })
}, 15000);
在我的云功能中
if (counter === 0 && state === 'CALL') {
call();
} else {
return;
}
这样,如果api在设定的时间内未完成订单,则api将调用该客户端。
关于javascript - Javascript函数如何在函数执行之前等待一段时间,如果这段时间内有某些更改,该函数将取最终值,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/54851516/