【问题标题】:Javascript function how to wait some time before a function executes which takes the final value if there is a certain change during that timeJavascript函数如何在函数执行之前等待一段时间,如果在此期间有一定的变化,则取最终值
【发布时间】:2019-02-24 11:41:26
【问题描述】:

所以基本上我有一个火力云 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 分钟后在最终值上执行。

【问题讨论】:

    标签: javascript function firebase-realtime-database google-cloud-functions vonage


    【解决方案1】:

    我做到了,我在客户端创建了一个计数器,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 将调用该客户。

    【讨论】:

    • 很高兴听到您成功了。您是否考虑在答案中包含实际的工作代码?这会让您的答案对未来的访问者更有用,并且更有可能获得支持。
    猜你喜欢
    • 2011-08-13
    • 1970-01-01
    • 2021-02-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多