"Abhorrent" in ajax sync request



For this reason, I wasted several hours.

Encountered in the work of such a demand:

Must wait for the follow-up after the completion of pre-task to be implemented, otherwise unable to obtain follow-up method to generate pre-mission data.

Pre-task and use them to the ajax request to the request of some of the background data for the use of follow-up methods.



The beginning of the idea:

function load () (

Pre-task (); / / using the ajax request, and set up the induction for the false.

Follow-up to the task (); / / read data to return to pre-task

)



Phenomenon:

FF: all normal, and occasionally some small problems, be less than the data

IE: can not access data

Summary of reasons:

Synchronization of the ajax request, and the concept of clear disruption of the proceedings.

Synchronous ajax request, and will not interrupt the operation of the current procedures.

Solution:

//定义一全局变量,数量为当前页面共发出的ajax请求数量
var requestCount = 1;
function load() {
   /*
    使用了ajax请求,并设置了异步为false.
    在ajax回调函数中,将requestCount--
    确保所有请求数据最后为0;
    */
    前置任务();
    //执行后续方法
    latter();
}
function latter() {
    /*
       如果请求数量为0,代表当前所有ajax请求已完成
       那可以进行后续任务,否则使用定时器,每隔500毫秒
       去重新执行一次这个方法
     */
    if (requestCount == 0) {
        //读取前置任务返回的数据
        后续任务();
    }else{
        window.setTimeout("latter()",500);
    }
}