$(window).unload 未按预期工作

发布于 2024-11-04 17:32:24 字数 998 浏览 2 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

攒眉千度 2024-11-11 17:32:24

问题是 $(window).unload() 在关闭窗口之前不会等待任何 AJAX 调用(这是正确的,因为 AJAX 是异步的)。

您需要强制 AJAX 同步,即等待响应。在 disconnectUser 函数中:

$.ajax({
    type: 'POST',
    async: false, // This is the guy.
    url: '/blablabla'
});

您可以在此处阅读有关它的更多信息:$(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:

$.ajax({
    type: 'POST',
    async: false, // This is the guy.
    url: '/blablabla'
});

You can read more about it here: $(window).unload wait for AJAX call to finish before leaving a webpage

已下线请稍等 2024-11-11 17:32:24

不用unload,用beforeunload 怎么样?

window.onbeforeunload = function() {
    if(connected){
        disconnectUser();
        connected = false;
    }
};

此外,您的 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?

window.onbeforeunload = function() {
    if(connected){
        disconnectUser();
        connected = false;
    }
};

Also, your disconnectUser method already sets connected to false, 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

蓝眼泪 2024-11-11 17:32:24

尝试使用同步请求。也许与其他海报建议的 onbeforunload 结合使用。如果这不起作用,我想你运气不好。同步请求在发生时会阻止浏览器,因此您可能只想将其用于卸载函数(假设该方法有效)。

function disconnectUser(){
            jQuery.ajax({
                url: 'web/WEB-INF/classes/handleChatUser.php',
                data: { action: 'disconnect',nick: localNickname},
                type: 'POST',
                async: false
            });

            $('#chat').stop(true,true).fadeOut(2000,function(){
                nicknameDialog();
            });

            $('#messageInput').val(null);
            $('#clientList').html(null);
            $('#chatScreen').html(null);
            clearInterval(refreshIntervalId);
            clearInterval(refreshIntervalId2);
            connected = false;
        }

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.

function disconnectUser(){
            jQuery.ajax({
                url: 'web/WEB-INF/classes/handleChatUser.php',
                data: { action: 'disconnect',nick: localNickname},
                type: 'POST',
                async: false
            });

            $('#chat').stop(true,true).fadeOut(2000,function(){
                nicknameDialog();
            });

            $('#messageInput').val(null);
            $('#clientList').html(null);
            $('#chatScreen').html(null);
            clearInterval(refreshIntervalId);
            clearInterval(refreshIntervalId2);
            connected = false;
        }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文