我在这里阅读了很多关于如何拦截窗口关闭和弹出对话框的例子。我需要不同的东西。在我的页面关闭或更改之前,我需要一个函数来调用和完成。
我已更新下面的代码,以便更好地解释我的问题。
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;
});
});
});
答案 0 :(得分:0)
要跟进my comment,这就是我的建议:
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请求并抢先取消它而不是在页面卸载之前发送请求。