Wait until all jQuery Ajax requests are done?

后端 未结 21 1801
离开以前
离开以前 2020-11-21 04:39

How do I make a function wait until all jQuery Ajax requests are done inside another function?

In short, I need to wait for all Ajax requests to be done before I exe

21条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-11-21 05:33

    Try this way. make a loop inside java script function to wait until the ajax call finished.

    function getLabelById(id)
    {
        var label = '';
        var done = false;
        $.ajax({
           cache: false,
           url: "YourMvcActionUrl",
           type: "GET",
           dataType: "json",
           async: false,
           error: function (result) {
             label='undefined';
             done = true;
            },
           success: function (result) {
                label = result.Message;
                done = true;
            }
         });
    
       //A loop to check done if ajax call is done.
       while (!done)
       {
          setTimeout(function(){ },500); // take a sleep.
       }
    
        return label;
    }
    

提交回复
热议问题