I am creating a small chat application with PHP + MySQL + JavaScript, I have written the disonnectUser () function, which is called when the user clicks the disconnect button. There he 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 function in a different context, when the user, instead of clicking, disables only the exit from the page, in this function
$(window).unload(function() {
if(connected){
disconnectUser();
connected = false;
}
});
he does not work. And I am sure that this is caused, because if I set a warning, it was usually called before closing the page. I think the page closes before the code runs completely, so I think that if I put some block there until the code finishes working, will it work?
source
share