2013-03-07 58 views
0

我有一個$ .getJSON請求不運行,但在請求執行後,代碼行正好運行。如果我刪除$ .getJSON請求後的所有代碼,請求將運行。如何獲取請求以運行對返回數據的迭代,然後在請求之後運行代碼。

var eventList = new Array(); 
$.getJSON('../index.php?/home/events', function(eventItems){  
    $.each(eventItems, function() { 
     var event = this; 
     var eventItem = new Array(); 
     // format the date and append to span 
     eventItem[0] = formatMDYDate(formatTimeStamp(this.loc_datetime, false), 0); 
     // add shortdescription to div 
     eventItem[1] = this.shortdescription; 

     // check if longdescription exist 
     if (this.longdescription) { 
      // create new anchor element for "More Info" link on events 
      var link = $('<a></a>'); 
      link.attr('href', '../index.php?/home/event_info'); 
      link.addClass('popup'); 
      link.html('More Info'); 
      //link.bind('click', eventPopUp()); 
      link.bind('click', function() { 
       var addressValue = event.id; 
       dialog = $('<div></div>').appendTo('body'); 
       dialog.load('../index.php?/home/event_info', 
        {id: addressValue}); 
       dialog.modal({ 
        opacity: 80 
       }); 
       return false; 
      }); 
      eventItem[2] = link; 
     } 
     eventList.push(eventItem); 
    }); 
}); 
// removing the following lines of code will let the .getJSON request run 
if (eventList.length > 0) { 
    write_Events(eventList); 
} 

我不知道是什麼原因導致這個問題,請大家幫忙!

回答

4

異步意味着,當你稱之爲JS運行庫不會等待它去fi在執行下一行代碼之前。通常情況下,您需要在這種情況下使用回叫。

它是這樣的:

var a="start"; 
setTimeout(function(){ 
a="done"; 
dosomethingWithA(a); 
},1000); 
if(a=="done"){}//doesn't matter, a is not "done" 
function dosomethingWithA(a){ 
// a is "done" here 
} 

在你的情況下,代碼應該是這個樣子:

var eventList = new Array(); 
$.getJSON('../index.php?/home/events', function(eventItems){  
    $.each(eventItems, function() { 
     var event = this; 
     var eventItem = new Array(); 
     // format the date and append to span 
     eventItem[0] = formatMDYDate(formatTimeStamp(this.loc_datetime, false), 0); 
     // add shortdescription to div 
     eventItem[1] = this.shortdescription; 
     // check if longdescription exist 
     if (this.longdescription) { 
      // create new anchor element for "More Info" link on events 
      var link = $('<a></a>'); 
      link.attr('href', '../index.php?/home/event_info'); 
      link.addClass('popup'); 
      link.html('More Info'); 
      //link.bind('click', eventPopUp()); 
      link.bind('click', function() { 
       var addressValue = event.id; 
       dialog = $('<div></div>').appendTo('body'); 
       dialog.load('../index.php?/home/event_info', 
        {id: addressValue}); 
       dialog.modal({ 
        opacity: 80 
       }); 
       return false; 
      }); 
      eventItem[2] = link; 
     } 
     eventList.push(eventItem); 
    }); 
    processEventList(); 
}); 
function processEventList(){ 
    // removing the following lines of code will let the .getJSON request run 
    if (eventList.length > 0) { 
    write_Events(eventList); 
    } 
} 
+0

謝謝,這個問題修復了。 – user2142672 2013-03-07 23:40:40

1

$.getJSON是一個異步調用。直到當前函數完全執行後,回調纔會執行。調用後的代碼將始終在getJSON回調運行之前運行。

其可能的write_Events函數拋出錯誤並停止執行,這就是回調永遠不會運行的原因。或者它實際上正在運行,但無論出於何種原因由額外的代碼調用,您都沒有看到它的證據。

3

嘗試

var eventList = new Array(); 
    $.getJSON('../index.php?/home/events', function (eventItems) { 
     $.each(eventItems, function() { 
      //.... 
      eventList.push(eventItem);    
     }); 
     // removing the following lines of code will let the .getJSON request run 
     if (eventList.length > 0) { 
      write_Events(eventList); 
     } 
    }); 

或者,你可以使用PubSub的使用jQuery技術

var eventList = new Array(); 
    $.getJSON('../index.php?/home/events', function (eventItems) { 
     $.each(eventItems, function() { 
      //.... 
      eventList.push(eventItem);    
     });    
     //publisher 
     $(document).trigger('testEvent', eventList); 
    }); 

    //subscriber 
    $(document).bind("testEvent", function (e, eventList) { 
     if (eventList.length > 0) { 
      write_Events(eventList); 
     } 
    }); 

更多detials http://www.codeproject.com/Articles/292151/PubSub-with-JQuery-Events

編碼快樂.. :)

+0

我想你把if語句放在.each主體中,如果它不是超出$ .each(eventItems函數體,所以eventList數組已被填充? – HMR 2013-03-07 04:30:46

+1

是的,eventList應該從.each ,我剛剛編輯謝謝 – Shahdat 2013-03-07 04:33:12

0

JavaScript代碼永遠不會等待服務器的響應,我們需要停止的處理JavaScript直到我們從服務器獲得響應。

我們可以通過jquery.Deferred

您還可以訪問this教程做到這一點。

+0

其實你可以通過設置async來等待響應:false – 2013-06-12 11:31:50

相關問題