在全球范围内配置所有$就要求以支持重试的超时范围内、重试、在全球

2023-09-10 16:08:26 作者:从青涩校服到纯白婚纱

我需要一种方法来设置全局某种超时功能与$ .ajaxSetup,让我PhoneGap的应用程序不断重试一个Ajax GET或POST每次有超时由于不良的互联网连接。

I need a way to set some kind of timeout function globally with $.ajaxSetup that will allow my Phonegap application to keep retrying an ajax GET or POST every time there is a timeout due to a bad internet connection.

我用Backbone.js的所以大部分的jQuery插件不会为这个工作,我会有些帮助写一个全局一块code将处理重试。

I use Backbone.js so most jquery plugins won't work for this, I would some help writing one global piece of code which will handle retries.

感谢你。

推荐答案

找到了解决方案,使所有AJAX调用一个重试超时工作。

Found a solution to make all AJAX calls work with a retry timeout.

$(document).ajaxError(function (e, xhr, options) {
    if(xhr.status == 0){
        setTimeout(function(){
            console.log('timeout, retry');
            $.ajax(options);
        }, 5000);
    }
});