0

我在这里阅读了很多关于如何拦截窗口关闭和弹出对话框的示例。我需要一些不同的东西。在我的页面关闭或更改之前,我需要调用和完成一个函数。

我在下面更新了我的代码,以帮助更好地解释我的问题。

window.addEventListener("beforeunload", function(event) {
  //I need the done callback function to fire before the page is changed
  //I've placed return null in that call because I think I need to pass something back?
  visualize({
    auth: {
      name: "piper",
      password: "password",
    }
  }, function(v) {
    //destroy session
    v.logout().done(function() {
      console.log("JRS Logout");
      return null;
    });
  });
});
4

1 回答 1

0

为了跟进我的评论,这就是我的建议:

visualize({
  auth: {
    name: "piper",
    password: "password",
  }
}, function(v) {
  //I need the done callback function to fire before the page is changed
  //I've placed return null in that call because I think I need to pass something back?
  window.addEventListener("beforeunload", function(event) {
    // make sure that this is reached
    console.log('JRS Logging Out');
    //destroy session
    v.logout().done(function() {
      // THIS WILL NEVER BE REACHED, but it should still work
      console.log("JRS Logout");
      return null;
    });
  });
});

我自己没有测试过这个,所以你不应该只相信它有效。确保在尽可能多的浏览器上对其进行测试,因为每个浏览器都可能会缓冲 XHR 请求并抢先取消它,而不是在页面卸载之前发送请求。

于 2018-02-16T21:08:19.290 回答