Asynchronous calls do not lead to synchronization problems

Business scenario: when clicking a button to save the data while opening a pop-out saved data

Based on this business scenario, recently I encountered a problem that is only found in ie11, click the button when the background break point plus a database query to verify the data is saved correctly, but the data is saved with less than pop page the problem is rather strange, gone through quite a long time, since the beginning of the problem ie to reproduce, and in 360 the browser speed mode is no problem, but when the first click did not bring out the data, the second click when can bring out the data, then it is easy to think of the problem ie cache, but transfer most of the day
plus ajax does not cache code has been changed to post requests, or get behind the connection request to add a time stamp parameters does not work

$(function(){
            //ajax不缓存请求结果
            $.ajaxSetup({cache: false});
        });

Then through communication and colleagues, only to find myself in the wrong direction, after examination revealed preservation method is to use asynchronous problem arises here, first verify that is not due to asynchronous cause of saving the data code and open popups page between code plus an alert prompt found that indeed, after association prompts alert pop, normal data out, the determination result is due to the asynchronous

Save code, note async:true,, here is asynchronous, may be considered before a performance problem, change asynchronous

$.ajax({
            url:'${root}/saveOrUpdate.do',
            type:"post",
            async:true,
            success:function(result){
            ...
            }
        });  

So after some testing, given their own programs, the solution is to use a callback function:

Save function:

function saveRecord(seq,callback){
    $.ajax({
            url:'${root}/saveOrUpdate.do',
            type:"post",
            async:true,
            success:function(result){
                if(callback!=undefined){
                        callback(true);
                }
            }
        });  
}
    //保存成功,才会打开弹窗
    function main(seq){
        saveRecord(seq,callbackFunction);
    
    }
    
    /*保存时的回调函数*/
     function callbackFunction(saveSuccess){
        if(saveSuccess){
            //省略打开弹窗代码
        }
     }

Guess you like

Origin www.cnblogs.com/mzq123/p/11402728.html