jquery getJSON function timing issue

后端 未结 4 1285
情深已故
情深已故 2021-01-12 08:57

I think my program is skipping result of JSON call. Is it possible to make a closure function here or make the program wait for JSON call to return?

function         


        
4条回答
  •  北恋
    北恋 (楼主)
    2021-01-12 09:33

    Another choice is to use a call back function and pass it to the function that executes the getJSON as:

    //this is the function that executes my getJSON
    //callback is the name of my callback function
    function getMyData( callback ){
    
      //here you do your getJSON call
      $.getJSON(url, function(items){
    
        //do your stuff here
    
        //call your function here (at the end)
        callback();
    
      });
    
    }
    

    This way your callback function will be called at the end of the getJSON call.

提交回复
热议问题