Javascript onunload function
Javascript onunload function
我读了很多关于这方面的文章,但我仍然搞不清楚。当用户离开我的页面时,我必须做一些工作。所以我用window.onforeload警告他,他将退出页面,而window.onfereload则启动我的工作。问题是,当我执行onunload函数时,在我的页面关闭之前,它不能正确完成。
我试过这个:
window.onunload = function() {
//freezeScreen(5000);
console.log("destroy finished 1/8");
$.each(_tabClassObj, function(index) {
_tabClassObj[index].destroy();
});
console.log("destroy finished 2/8");
_tabClassObj = {};
console.log("destroy finished 3/8");
_terminalTabCounter = 0;
console.log("destroy finished 4/8");
window.onbeforeunload = null;
console.log("destroy finished 5/8");
window.onunload = null;
console.log("destroy finished 6/8");
}
这是我的控制台日志:
destroy finished 1/8
WebSocket is already in CLOSING or CLOSED state.
WebSocket is already in CLOSING or CLOSED state.
WebSocket is already in CLOSING or CLOSED state.
WebSocket is already in CLOSING or CLOSED state.
WebSocket is already in CLOSING or CLOSED state.
我试图用冻结功能延迟浏览器UI关闭,该功能将浏览器关闭延迟5秒……但仍然不起作用。我还在这里尝试了setTimeout。。。但是具有相同的结果。问题出在哪里?谢谢你的建议。
如果您使用Blaze
作为视图层,那么您可以访问onDestroy
方法,在离开url之前编写您想要放置的逻辑。
Template.xxx.onDestroy( function() {
// logic goes here.
});
我无法用javascript事件完成我想要的操作。提到我的流星服务器,我的客户关闭了他的浏览器,我开发了一个这样的ping系统:
// Both side
TelnetUserConnections = new Meteor.Collection('TelnetUserConnections');
// Client side
Meteor.startup(function(){
// Timer who refresh the heartbeat on server
Meteor.setInterval(function () {
Meteor.call('keepAlive', Blaze._globalHelpers.currentUserId());
}, 5000);
});
// Server side
Meteor.methods({
"keepAlive": function (user_id) {
if (!TelnetUserConnections.findOne({user_id: user_id})) {
TelnetUserConnections.insert({user_id: user_id});
}
TelnetUserConnections.update({user_id: user_id}, {$set: {last_seen: (new Date()).getTime()}});
}
});
Meteor.setInterval(function () {
// do the job when user hasn't pinged since 20 secondes
var now = (new Date()).getTime();
TelnetUserConnections.find({last_seen: {$lt: (now - 20 * 1000)}}).forEach(function (user) {
/* here is my destroy stuff */
TelnetUserConnections.remove({_id : user._id});
});
});
也许它可以帮助
相关文章:
- 直接在函数声明上使用function.prototype.bind
- jQuery:.click(function(){(element),collapse('show',f
- 如果连接类型为none,则Javascript Function Only警报
- onLoad function phonegap android
- reducers在redux中得到Function not Object,what'it’他错了
- WIll window.addEventListener(“加载”,function(),false);等待浏览器自动填
- onunload如何正常工作
- $(document).ready和jQuery(function($)不工作;jQuery已定义,但脚本尚未定义;t进
- Javascript onunload function
- 在init function()中调用getElementById(“..”),而不是想用它做点什么的函数
- spyOn:应为间谍,但得到了Function
- (typeof variable === “function”) 和 jQuery.isFunction() 有什么区别
- this内部的值返回this.each(function(){})
- 如何在JavaScript中创建类似于something.function()的函数
- 如何通过 Function.prototype.apply() 更改调用函数的参数
- 控制台中的“function floor(){[本机代码]}”是什么
- ASPX Jquery 1.11 $(Document).ready(function() {} was skipped
- Function.prototype.call和Function.protoype.all只应用一个参数
- jQuery onunload在按下按钮时执行操作
- 为什么window.open(…)Onunload = function () {..不像我想象的那样起作用