2013-04-07 17 views
0

我有一個ul其中包含許多鏈接。當使用jQuery加載這些鏈接時,它會同時渲染它們。我怎樣才能加載它們一個接一個,他試圖如下:JQuery加載函數爲一組鏈接逐一?

JS:

$('#publish').click(function() { 
    $('#get_links li'). 
     each(function() { 
      $(this).load('test.php',{'post':$(this).text()}); 
      $(this).html("<i class='icon-spin icon-spinner'></i>Loading"); 

      ///NEED TOSLEEP UNTILL $.load finish.. 
     } 
     ); 
    } 
); 

我怎麼能睡覺。每個循環,直到.load結束?

+0

可能重複[繼續執行後才。每()完成(http://stackoverflow.com/questions/9043968/continue-execution-只有在完成後) – karthikr 2013-04-07 21:03:07

+0

我認爲這些問題是足夠獨特的 - 你引用的一個並不需要用'$()。load()'。 – jmar777 2013-04-07 21:09:41

回答

2

您可以使用此$().load回調:中

$('#publish').click(function(){ 
    var $link = $('#get_links li').first(); 

    var loadLink = function() { 
     $link.load('test.php', { post: $link.text() }, function() { 
      // once loading is done, grab the next link... 
      $link = $link.next(); 
      // ... and load it if we found one 
      if ($link.length) loadLink(); 
     }).html('<i class="icon-spin icon-spinner"></i>Loading'); 
    }; 

    // load the first link 
    loadLink(); 
}); 
+0

哇..從來沒有想過使用.next()! :)。多數民衆贊成在此完美謝謝 – Zalaboza 2013-04-07 21:13:30

+0

沒問題,很高興它幫助! – jmar777 2013-04-07 21:19:20