最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - PHP update database on close window - Stack Overflow

programmeradmin1浏览0评论

I am trying to update the database when the user close or refresh the browser. This is my code:

$(window).bind('unload', function(){
    $.ajax({
        type: 'get',
        async: false,
        url: 'liberaLugar.php?idCurso=2'
    });
});

Any help will be appreciated. Thanks in advance!

I am trying to update the database when the user close or refresh the browser. This is my code:

$(window).bind('unload', function(){
    $.ajax({
        type: 'get',
        async: false,
        url: 'liberaLugar.php?idCurso=2'
    });
});

Any help will be appreciated. Thanks in advance!

Share edited Nov 30, 2014 at 20:15 r3wt 4,7422 gold badges36 silver badges56 bronze badges asked Nov 30, 2014 at 19:31 Andres CavazosAndres Cavazos 611 silver badge8 bronze badges 0
Add a ment  | 

1 Answer 1

Reset to default 6

This should work for you in most cases. of course, if your server is unreachable, you could freeze the browser, so we should also add a timeout. it may also be helpul to give your user some visual indicator of what the hell is going on, so we should set the cursor to waiting.

var unloaded = false;
$(window).on('beforeunload', unload);
$(window).on('unload', unload);  
function unload(){      
    if(!unloaded){
        $('body').css('cursor','wait');
        $.ajax({
            type: 'get',
            async: false,
            url: 'liberaLugar.php?idCurso=2',
            success:function(){ 
                unloaded = true; 
                $('body').css('cursor','default');
            },
            timeout: 5000
        });
    }
}

EDIT: Synchronous AJAX is now deprecated in Webkit Browsers.

发布评论

评论列表(0)

  1. 暂无评论