jq如何实现关闭浏览器后仍然执行定时器
1、先说兼容,找到了以下代码,提示用户是否离开此页面(关闭、刷新或者点击后退等)window.addEventListener("beforeunload", function (e) { var confirmationMessage = '确定离开此页吗?本页不需要刷新或后退'; (e || window.event).returnValue = confirmationMessage; // Gecko and Trident return confirmationMessage; // Gecko and WebKit});

2、再说浏览器页面关闭之后做的事 因为这个函数的确认,和取消都是别人给的。你无法去操作 可以改用另外一种方式:<html><head><script type="text/javascript"> function show_confirm()

3、{ var r=confirm("要离开此页吗!"); if (r==true) { alert("你同意了!"); //关闭前干掉别的 window.close();//关闭 } else { alert("你不同意!");// //能做点别的 } } </script></head><body><input type="button" onclick="show_confirm()" value="Show a confirm box" /></body></html>

4、你也可以换个思路在显示关闭前执行自己的函数,但是如果自己的函数是发请求或其他异步操作一旦用户点击离开页面,浏览器默认会把请求aborted。

5、window.onbeforeunload = function (event) { dosomething();// 执行自己的函数 var message = '...'; if (typeof event == 'undefined') { event = window.event; } if (event) { event.returnValue = message; } return message; }
