使用beforeunload在页面退出之前执行任务 - Javascript / jQuery [英] Using beforeunload to execute task before page exit - Javascript/jQuery

查看:86
本文介绍了使用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..

这篇关于使用beforeunload在页面退出之前执行任务 - Javascript / jQuery的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆