How can I make jquery wait for one function to finish before executing another function?

前端 未结 1 1092
长发绾君心
长发绾君心 2021-02-13 00:19
function test(){
var distance=null;
       first();
       second();
       third();
alert(distance);//it shows null always because it take 2 second to complete.

}
 fun         


        
1条回答
  •  醉酒成梦
    2021-02-13 01:02

    Make use of callbacks:

    function first(tolat, tolon, fromlat, fromlon, callback) {
         if (typeof(callback) == 'function') {
            callback(distance);
         }
    }
    
    function second() { }
    function third() { }
    
    first("vartolat", "vartolon", "varfromlat", "varfromlon", function(distance) {
        alert(distance);
        second();
        third();
    });
    

    0 讨论(0)
提交回复
热议问题