jquery, how to run a function as soon as another function ends

前端 未结 6 1856
梦谈多话
梦谈多话 2021-01-12 18:58

*Note: Re-writing question:

I am trying to write the following outside of the individual function calls:

example:

function f1(){
         


        
6条回答
  •  日久生厌
    2021-01-12 19:02

    Deferreds are an option. You could always do the following:

    $.when(firstFunction()).then(secondFunction);
    

    The only trick is that inside firstFunction, you would need to do the following:

    function firstFunction(){
        var deferred = $.Deffered();
    
        $.get(url,function(r){
            deferred.resolve();
        });
    
        return deferred;
    }
    

    The when function will wait until the deferred returned by the firstFunction call is resolved. Once the deferred is resolved (in the ajax success callback) the '.then' statement will fire, calling your secondFunction.

    The jQuery docs explain the Deferred API very well.

提交回复
热议问题