延迟返回等待异步函数(beforeunload事件)
Delay Return to Wait for Asynchronous Functions (beforeunload event)
在这个编码示例中,函数logout()
不会执行所有的异步调用,并且不会等到它们完成-相反,页面在此之前正在卸载,因为beforeunload
事件的返回触发了卸载页面。
$(window).on('beforeunload', function(event) {
event.preventDefault();
logout();
return;
});
我想尝试的是事件函数在logout()
中的几个异步调用完成后返回。
我尝试了回调,但最终得到了这个,这不是期望的结果,因为它返回回调,而不是事件函数。
$(window).on('beforeunload', function(event) {
event.preventDefault();
logout(function(x) { return; });
});
由于页面上执行的所有内容在页面卸载时都将失效,因此您不能依赖页面本身来完成异步调用。
chrome扩展的一个词是利用背景页。您可以简单地将消息发送到beforeunload
处理程序中的后台页面,捕获您需要处理的所有信息,并在后台页面中执行异步调用。示例代码如下:
content.js
window.addEventListener('beforeunload', function() {
chrome.runtime.sendMessage({ info: "Here is the info you would like to pass to background page"});
});
background.js
chrome.runtime.onMessage.addListener(function(request) {
// The following is your async call
logout();
// Don't forget the following code
return true;
});
不要忘记从后台页面的事件侦听器返回true,因为当事件侦听器返回时chrome.runtime.onMessage.addListener
将无效,详细信息请参见此答案。
尝试使用async/await
作为处理程序,即:
$(window).on('beforeunload', async function(event) {
await logout();
event.preventDefault();
event.returnValue = false; // Seems require this for Chrome
});
当然,您应该从logout()
返回Promise
;
但我不确定这是否可靠
不是一个干净的解决方案,但是您可以尝试setTimeout来强制代码在注销进行时等待。
var timeToLogoutInMs = 500;
setTimeout(function() {
// Nothing
}, timeToLogoutInMs);
编辑:为什么你需要在beforeunload钩子上做这个?为什么不设置一个手动选项用户退出? 相关文章:
- 分派点击事件并保留击键修饰符
- 模糊事件的Javascript测试
- keyup事件处理程序更改焦点不适用于快速键入
- 使用 jQuery 的 .on 函数如何获取事件的原始元素
- 如何使Javascript动态html表及其上的事件
- 使用类从一个标记中双击事件
- 如何在javascript中更改beforeunload事件
- jQuery beforeunload 事件未在后退按钮上触发
- 如何阻止 标记的 window.on('beforeUnload') 事件
- 从beforeunload事件中排除提交按钮
- 是否有任何方法可以检测用户是否按下“按钮”;停留在页面上”;或“;离开页面"在beforeunload事件中
- JavaScript & # 39; beforeunload& # 39;事件不能在ie中工作
- 用于cookie删除和页面刷新的Beforeunload事件
- 如何使beforeunload事件仅在浏览器会话退出时触发?
- 查询beforeunload事件的数据库
- 在Firefox上,beforeunload事件不会触发
- 移除beforeunload的事件监听器
- FireFox不会为window beforeunload事件显示任何警告对话框
- Firefox没有'除非单击页面,否则不要调用“beforeunload”事件
- 延迟返回等待异步函数(beforeunload事件)