使用beforeunload在页面退出之前执行任务 - Javascript / jQuery

使用beforeunload在页面退出之前执行任务 -  Javascript / jQuery

问题描述:

我正在尝试在页面离开之前执行一个操作,这需要执行GET请求。

我想给GET请求至少 50-100ms ,以便将请求转发到Web服务器。

I would like to give the GET request at least 50-100ms in order to get the request forwarded to the web server.

我正在捕获 beforeunload 操作使用:

$(window).bind('beforeunload', function() {
    send_data();
});






任何提示?


Any tips?

如果您同步发送请求发送请求的最高机会( async:false $。ajax()选项,如果你使用jQuery)。

You have the highest chance to get your request sent if you make it synchronous (async: false in the $.ajax() options if you are using jQuery).

但你不能确定它总是通过..

But you cannot be sure it always gets through..