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

时间:2019-01-13 08:34:52

标签: javascript html css firebase firebase-realtime-database

当前,我正在使用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.");
});  

但是这里的问题是显示警报或页面正在刷新。两者不能同时工作。因此,我的问题是如何显示警报并一次又一次刷新表单页面?

2 个答案:

答案 0 :(得分:1)

尝试此操作,如果用户在警报中单击ok,则会重新加载窗口:

if(!alert("Thank You! Your request has been received and our team will connect with you shortly.")){
  window.location.reload();
}

答案 1 :(得分:1)

alert函数是一个阻止函数。这意味着您在运行时:

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

如果尝试这样做,您将看到hello again不会被记录,直到您在警报弹出窗口中按OK为止。

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

// 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();
});