ajax加载json文件直到崩溃

时间:2018-01-25 15:05:45

标签: javascript jquery ajax

我有一小段javascript,用ajax加载json文件,然后决定从那里做什么。该函数从一个区间调用,因为它需要每隔x秒运行一次,在这种情况下,每30秒运行一次。

jQuery(document).ready(function () {
    loadBuildInterval = setInterval(loadBuildData, intervalTime); //Start interval when document is loaded
});

function loadBuildData() {
    jQuery.ajax({
        url: fileUrl + '?v' + (Math.round(new Date()/1000)), //Add unique version to json file every time it is requested (cache busting).
        type: 'POST',
        data: data,
        dataType: 'json',
        success: function (result) {
            if (result !== null) { //If the json file is not empty, continue
                if (userActiveBuild === 'loading') { //Set initial active build. This will be the build the user loaded into
                    userActiveBuild = result['currentBuild'];
                }
                dataLoaded(result); //Start comparing build
                setTimeout(function () {
                    if (result['inScript'] !== '0') { //If a script must be loaded, run function for it
                        inScript(result);
                    }
                    if (result['inStyle'] !== '0') { //If a style must be loaded, run function for it
                        inStyle(result);
                    }else{
                        jQuery("link[href*='"+scriptURL+"css/inStyle.css']").remove();
                    }
                }, inScriptPause);
                if (intervalTime !== result['userTestInterval']) { //Update interval to the one set in the wp settings
                    intervalTime = result['userTestInterval'];
                    clearInterval(loadBuildInterval); //Reload interval with new duration
                    loadBuildInterval = setInterval(loadBuildData, intervalTime);
                }
            }else{
                //No json data.
            }
        },
        error: function () {
            clearInterval(loadBuildInterval);
            console.log('Reload Changes - Aborting - ajax error.');
        }
    });
}

好吧我的问题是"错误:function()"的ajax请求。 看起来有时候(有时候并不总是有时)函数会给出错误,我想也许它无法加载文件,但是当发生这种情况时,ajax函数会尽可能快地自我保护,并且用户可以使用浏览器将冻结,我的服务器将以100%运行。

我需要ajax在遇到错误时退出,我该怎么做?

由于

1 个答案:

答案 0 :(得分:1)

我建议使用setTimeout代替setInterval, 试试这个:

jQuery(document).ready(function () {
    loadBuildData();
});

function loadBuildData() {
    jQuery.ajax({
        url: fileUrl + '?v' + (Math.round(new Date()/1000)), //Add unique version to json file every time it is requested (cache busting).
        type: 'POST',
        data: data,
        dataType: 'json',
        success: function (result) {
            if (result !== null) { //If the json file is not empty, continue
                if (userActiveBuild === 'loading') { //Set initial active build. This will be the build the user loaded into
                    userActiveBuild = result['currentBuild'];
                }
                dataLoaded(result); //Start comparing build
                setTimeout(function () {
                    if (result['inScript'] !== '0') { //If a script must be loaded, run function for it
                        inScript(result);
                    }
                    if (result['inStyle'] !== '0') { //If a style must be loaded, run function for it
                        inStyle(result);
                    }else{
                        jQuery("link[href*='"+scriptURL+"css/inStyle.css']").remove();
                    }
                }, inScriptPause);

                if (intervalTime !== result['userTestInterval']) { //Update interval to the one set in the wp settings
                    // Change interval
                    intervalTime = result['userTestInterval'];
                }
            }else{
                //No json data.
            }
            // Recall the function after intervalTime
            setTimeout(loadBuildData, intervalTime); 
        },
        error: function () {
            console.log('Reload Changes - Aborting - ajax error.');
        }
    });
}