简体   繁体   English

是否可以在 firebase 云函数中使用 setTimeout()?

[英]is it possible to use setTimeout() in firebase cloud functions?

i am wondering if it is possible to use setTimeout() in cloud functions我想知道是否可以在云函数中使用 setTimeout()

let millisecond = (day *24*60*60*1000) + (hour* 60*60*1000) + (min *60*1000) + (second *1000);

setTimeout(() => {
    admin.firestore().doc("timer/" + chatroom).set({
        "millisecond":millisecond,
        "username":username,
        "tousername":tousername,
        "day":day,
        "min":min,
        "second":second,
        "hour":hour,
    
}, millisecond);

if not possible is there a way to accomplish this idea?如果不可能,有没有办法实现这个想法? also am i billed as much as the function is active? function 活跃时我是否也需要支付费用? thank you谢谢你

Using setTimeout in a Cloud Function is possible, but keep in mind that:在 Cloud Function 中使用setTimeout是可能的,但请记住:

  • a function can run at most 9 minutes (1st generation) or 60 minutes (2nd generation) (see docs ) function 最多可以运行 9 分钟(第一代)或 60 分钟(第二代)(参见文档
  • you pay for the entire time a function is active this way您支付 function 以这种方式激活的整个时间

So if you need to wait for significant time (I usually use 30s as a threshold for this), consider using another mechanism to delay operations such as scheduling a callback with Cloud Tasks .因此,如果您需要等待很长时间(我通常使用 30 秒作为阈值),请考虑使用另一种机制来延迟操作,例如使用 Cloud Tasks 安排回调

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM