onbeforeunload wait for ajax result

前端 未结 4 1441
梦谈多话
梦谈多话 2021-01-23 15:32

Is it possible for onbeforeunload() function to wait for ajax result and move to a jsp based on the resultvalue or atleast show a message before unloading?

4条回答
  •  粉色の甜心
    2021-01-23 16:20

    This is my strategy and works for me:

    _number_of_active_ajax ++;
    $.ajax({
        url: REQUEST_URL,
        timeout: 3000
    })
    .always(function() {
        _number_of_active_ajax --;
    });
    
    window.onbeforeunload = function() {
        if(_number_of_active_debounce > 0 || _number_of_active_ajax > 0)
            return "Your question";
    }
    

    }

    It will show a message to use if the page has an active request.

提交回复
热议问题