3

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?

tshepang
  • 12,111
  • 21
  • 91
  • 136
Rodrigo Cavalcante
  • 1,577
  • 4
  • 14
  • 21

3 Answers3

6

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

Community
  • 1
  • 1
Erick Petrucelli
  • 14,386
  • 8
  • 64
  • 84
  • 1
    If I have fully answered your question, **please mark it as accepted**. If something still remains, let us know so we can complete the answers. – Erick Petrucelli May 04 '11 at 13:56
2

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

Eli
  • 17,397
  • 4
  • 36
  • 49
1

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;
        }
nitro2k01
  • 7,627
  • 4
  • 25
  • 30