firebase cloud功能在10秒后运行



我有一个类似于下面的函数。当用户提交申请时触发。寻找合适的匹配与另一个用户。如果没有找到匹配,我希望它在10秒后再次检查。我想让它调用函数"deleteReference(gameid)"。这个函数也会再次检查。如果没有其他用户匹配该引用,该引用将被删除。

在一些问题中,他们讨论了一个有延迟的解决方案,但他们等待的时间得到了报酬。我如何在10秒后触发我想要的函数(通过发送gameID变量)?以最实惠的价格。

exports.myFunction = functions.database.ref('/match/{gameid}/').onCreate((snapshot, context) => {
//do some processing here

if (matchResult == true) {
return null;
} else {
// HOW CAN I CALL THIS FUNCTION AFTER 10 SEC ?
deleteReference(gameid)
return null;
}
});
function deleteReference(gameid) {
//do some processing here
if (matchResult == false) {
database.ref("/match/").child(gameid).remove();
}
}

一个选择是通过使用云任务作为@samthecodingman链接。您还可以使用setTimeout()来延迟deleteReference()函数的执行。参见下面的示例代码:

exports.myFunction = functions.database.ref('/match/{gameid}/').onCreate((snapshot, context) => {
//do some processing here

if (matchResult == true) {
return null;
} else {
// Execute the function after 10 seconds
setTimeout(deleteReference(gameid), 10000)
return null;
}
});
function deleteReference(gameid) {
//do some processing here
if (matchResult == false) {
database.ref("/match/").child(gameid).remove();
}
}
exports.example = functions..({
setTimeout(() => {
//Your code
}, "milliseconds here");
});

最新更新