开发者

JavaScript callback function

I have this jQuery script:

$(content).find('data').each(function(){

  // parsing some data

  $(this).find('something').each(function(){

    $(this).find('something new').each(func开发者_运维技巧tion(){

      // etc.

    }};

  });

});

...and I want to call function XYZ() after this script has finished.

This isn't working (it will call function XYZ() too early):

$(content).find('data').each(function(){

  // parsing some data



}, XYZ());

Are there any solutions? Thank you.


$.each isn't asynchronous, so you don't need a callback per se. This will work:

$(content).find('data').each(function(){
  // parsing some data
  $(this).find('something').each(function(){

    $(this).find('something new').each(function(){

    // etc.
    });
  }};
});

XYZ();


in case you have any asynchronous process, you should call "XYZ ()" in "/ / etc."

var xData $(content).find('data'),
    lenData = $(xData).length;

if (lenData==0){
  XYZ();
}
else{
  $.each(xdata, function(index, value){
    // parsing some data

    var xsomething = $(this).find('something')
        lenxsomething = $(xsomething).length;

    if ((lenxsomething==0) && (lenData==(index+1))){
       XYZ();
    }
    else{
      $.each(xsometing, function(index2, value2){

        var xsomethingNew $(this).find('something new')
            lenxsomethingNew = $(xsomething).length;

        if ((lenxsomethingNew==0) && (lenxsomething==(index2+1))){
           XYZ();
        }
        else{
         $.each(xsomethingNew, function(index3, value3){
              //etc ..
              //then asynchronous process
              if (lenxsomethingNew==(index3+1)){
                 XYZ();
              }
          });
        }

      });
    }
  });
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜