如何prevent页面导航,直到AJAX调用完成 [英] How to prevent page navigation until ajax call is complete

查看:107
本文介绍了如何prevent页面导航,直到AJAX调用完成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我有一个Ajax调用打倒几十块数据的所有数百万字节,随后通过HTML5文件系统API本地存储的数据。

So I have an ajax call to bring down several dozen chunks of data all several megabytes in size, afterward storing the data locally via the html5 filesystem api.

我想prevent用户从页面导航离开阿贾克斯电话都是做过。我决定去探索 onbeforeunload的事件,把它通知用户应该留在页面上,直到Ajax调用完成。我设置了AJAX调用之前和之后在AJAX的结束/成功叫我重置 window.onbeforeunload

I wanted to prevent the user from navigating away from the page before the ajax calls were done. I decided to explore the onbeforeunload event, to have it notify that the user should stay on the page until the ajax calls are complete. I set the following before the AJAX call and at the end/success of the AJAX call I reset the window.onbeforeunload.

window.onbeforeunload = function(){
    return "Information is still downloading, navigating away from or closing "+ 
        "this page will stop the download of data";
}

当我试图关闭该页面或导航到该页面时,弹出消息出现如预期,通知留用户。但是,一旦我证实,我想留在页面上,Ajax调用不恢复他们离开的地方。有没有一种方法,以prevent从暂停Ajax调用/停止或继续/重新启动他们的执行?

When I attempted to close the page or navigate away from the page, the pop-up message comes up as expected informing the user to stay. However, once I confirm that I want to stay on the page, the ajax calls do not resume where they left off. Is there a way to prevent the ajax calls from pausing/stopping or to continue on/restart with their executions?

我接受任何想法,以实现在这篇文章的标题描述所需的功能。

I'm open to any ideas to implement desired functionality described in the title of this post.

推荐答案

pretty的肯定,你可以在创建一个全球性的的.js 文件中像...

Pretty sure you can create a global in the .js file like...

VAR请求;

那么你的AJAX调用分配给该变量。

Then assign your ajax call to this variable.

request = $.ajax{
 //Ajax
 //Stuff
 //Goes
 //Here
}

现在你的 window.unbeforeunload 函数中,添加此条件语句。

Now inside your window.unbeforeunload function, add this conditional statement.

window.onbeforeunload = function(){
  if(!request){
    return "Request not initiated";
  }else{
    //Request is in progress...
    //You can use request.abort() if you need
  }
}

编辑:要在一些你可以在要求对象的看看这个页面。 (例如, .done 永远可能适合您的情况)

To elaborate on on some of the methods you can use on the request object, check out this page. (for example, .done or .always may suit your circumstances)

这篇关于如何prevent页面导航,直到AJAX调用完成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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