gpt4 book ai didi

javascript - 如何在 Firebase 响应后同时显示警报和刷新页面?

转载 作者:太空宇宙 更新时间:2023-11-04 00:57:44 25 4
gpt4 key购买 nike

目前我正在使用 firebase 存储数据,一旦我收到 firebase 的响应,我需要显示警报并刷新表单页面。这是我为此目的使用的代码:

  // Push a new inquiry to the database using those values
inquiry.push({
"name": name,
"contact": contact,
"email": email,
"board": board,
"subject": subject,
"standard": standard,
"message": message,
}).then(() => {
alert("Thank You! Your request has been received and our team will connect with you shortly.");
});

但这里的问题是显示警报或正在刷新页面。两者不同时工作。那么,我这里的问题是如何显示alert并依次刷新表单页面?

最佳答案

alert 函数是一个阻塞函数。这意味着当你运行时:

alert("hello"); 
console.log("hello again");

如果您尝试一下,您会发现 hello again 不会被记录,直到您在警报弹出窗口中按下 ok。

这意味着您可以将导航代码放在 then() 中的 alert 之后:

// Push a new inquiry to the database using those values
inquiry.push({
"name": name,
"contact": contact,
"email": email,
"board": board,
"subject": subject,
"standard": standard,
"message": message,
}).then(() => {
alert("Thank You! Your request has been received and our team will connect with you shortly.");
location.reload();
});

关于javascript - 如何在 Firebase 响应后同时显示警报和刷新页面?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54167260/

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