$(window).unload 未按预期工作
我正在使用 PHP + MySQL + JavaScript 制作一个小型聊天应用程序,我编写了一个函数 disonnectUser(),当用户按下断开连接按钮时会调用该函数。它是这样的:
function disconnectUser(){
$.post('web/WEB-INF/classes/handleChatUser.php',{ action: 'disconnect',nick: localNickname});
$('#chat').stop(true,true).fadeOut(2000,function(){
nicknameDialog();
});
$('#messageInput').val(null);
$('#clientList').html(null);
$('#chatScreen').html(null);
clearInterval(refreshIntervalId);
clearInterval(refreshIntervalId2);
connected = false;
}
它的工作原理就像一个魅力,但是当我在另一个上下文中调用这个函数时,当用户而不是按下断开连接只是退出页面时,在这个函数中
$(window).unload(function() {
if(connected){
disconnectUser();
connected = false;
}
});
它不起作用。我确信它正在被调用,因为如果我发出警报,它会在关闭页面之前正常调用。我认为页面在代码完全运行之前关闭,所以我想如果我在那里放置一些块直到代码完成运行它会起作用吗?
I'm making a small chat application with PHP + MySQL + JavaScript, I've written a function disonnectUser(), which is called when the user press the disconnect button. Here it is:
function disconnectUser(){
$.post('web/WEB-INF/classes/handleChatUser.php',{ action: 'disconnect',nick: localNickname});
$('#chat').stop(true,true).fadeOut(2000,function(){
nicknameDialog();
});
$('#messageInput').val(null);
$('#clientList').html(null);
$('#chatScreen').html(null);
clearInterval(refreshIntervalId);
clearInterval(refreshIntervalId2);
connected = false;
}
And it works like a charm, but when I call this very function in another context, when the user instead of pressing disconnect just exit the page, in this function
$(window).unload(function() {
if(connected){
disconnectUser();
connected = false;
}
});
it doesn't work. And I'm sure it's being called, because if I put an alert it's called normally before closing the page. I think the page is closing before the code runs completely, so I think if I put some block there until the code finish running it would work?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
问题是
$(window).unload()
在关闭窗口之前不会等待任何 AJAX 调用(这是正确的,因为 AJAX 是异步的)。您需要强制 AJAX 同步,即等待响应。在
disconnectUser
函数中:您可以在此处阅读有关它的更多信息:$(window).unload 在离开网页之前等待 AJAX 调用完成
The problem is that
$(window).unload()
doesn't waits any AJAX call before closing the window (what is right because AJAX is assync).You need to force the AJAX to be sync, ie, wait the response. Inside your
disconnectUser
function:You can read more about it here: $(window).unload wait for AJAX call to finish before leaving a webpage
不用unload,用beforeunload 怎么样?
此外,您的
disconnectUser
方法已将connected
设置为false
,也无需在此处执行此操作。jQuery 似乎也没有真正处理
beforeunload
事件,这就是为什么您需要恢复到本机 JS 来处理此事件:http://groups.google.com/group/jquery-en/browse_thread/thread/4e5b25fa1ff5e5ee?pli=1< /a>
Instead of unload, how about beforeunload?
Also, your
disconnectUser
method already setsconnected
tofalse
, no need to do it here also.It also seems that jQuery doesn't really handle the
beforeunload
event, which is why you'll need to revert to native JS to handle this:http://groups.google.com/group/jquery-en/browse_thread/thread/4e5b25fa1ff5e5ee?pli=1
尝试使用同步请求。也许与其他海报建议的
onbeforunload
结合使用。如果这不起作用,我想你运气不好。同步请求在发生时会阻止浏览器,因此您可能只想将其用于卸载函数(假设该方法有效)。Try using a synchronous request. Perhaps in combination with
onbeforunload
like the other poster suggested. If that doesn't work, I suppose you're out of luck. A request that is synchronous blocks the browser while it's happening, so you might want to use it only for the unload function, assuming the method even works.