2012-11-02 39 views
1

我有我想一次加載一個函數的列表。我無法讓它們加載一個,然後在加載數據後再轉到下一個。這裏是我的代碼:用jQuery一次加載一個函數?

$(document).ready(function() { 
    //step through the data sets 
    var count = 1; 
    if (count = 1) { 
     loadingAjax('connectConferenceData','connectAttendanceData',count); 
    } 
    if (count = 2) { 
     loadingAjax('connectWorkshopData','cwsData',count); 
    } 
    if (count = 3) { 
     loadingAjax('relayCenterData','rcData',count); 
    } 
    if (count = 4) { 
     loadingAjax('collectionCenterData','ccData',count); 
    } 
    if (count = 5) { 
     loadingAjax('regionalStatsData','rsData',count); 
    } 
    if (count > 5) { 
     $("#statusMsg").html('All tools have been loaded.').fadeIn('slow'); 
     setTimeout(function() { 
      $('#statusMsg').fadeOut(); 
     }, 10000); 
    } 
}); 

//function to get datasets 
function loadingAjax(div_id,action_id,count) { 
    $("#loading").show(); 
    $("#"+div_id).html('<img src="images/admin_uploading.gif"> Loading data...'); 

    $.ajaxSetup ({ 
     cache: true 
    }); 

    $.ajax({ 
     type: "GET", 
     url: "dashboard_functions.php", 
     data: "actionID="+action_id, 
     success: function(data){ 
      $("#"+div_id).html(data).fadeIn(); 
      count++; 

      if (count != 6) { 
       $("#statusMsg").html('<img src="./images/admin_uploading.gif" /> Loading data for tool #'+count+' loading'); 
      } 

      $("#loading").hide(); 
     } 
    }); 
} 
+2

上先看看你需要使用「==」而不是在所有if語句中使用'=' –

+0

甚至===強制沒有類型強制,或者只是刪除它,並把正確的調用放在你的成功處理程序 – NimChimpsky

回答

0

你可以建立這樣的一系列的回調:

//step through the data sets 
function DataSetStep(count){ 
if (count == 1) { 
    loadingAjax('connectConferenceData','connectAttendanceData',count); 
} 
if (count == 2) { 
    loadingAjax('connectWorkshopData','cwsData',count); 
} 
if (count == 3) { 
    loadingAjax('relayCenterData','rcData',count); 
} 
if (count == 4) { 
    loadingAjax('collectionCenterData','ccData',count); 
} 
if (count == 5) { 
    loadingAjax('regionalStatsData','rsData',count); 
} 
if (count > 5) { 
    $("#statusMsg").html('All tools have been loaded.').fadeIn('slow'); 
    setTimeout(function() { 
     $('#statusMsg').fadeOut(); 
    }, 10000); 
} 
} 

$(document).ready(function() { 
DataSetStep(1); 
}); 

//function to get datasets 
function loadingAjax(div_id,action_id,count) { 
$("#loading").show(); 
$("#"+div_id).html('<img src="images/admin_uploading.gif"> Loading data...'); 

$.ajaxSetup ({ 
    cache: true 
}); 

$.ajax({ 
    type: "GET", 
    url: "dashboard_functions.php", 
    data: "actionID="+action_id, 
    success: function(data){ 
     $("#"+div_id).html(data).fadeIn(); 
     count++; 

     if (count != 6) { 
      $("#statusMsg").html('<img src="./images/admin_uploading.gif" /> Loading data for tool #'+count+' loading'); 
     } 

     $("#loading").hide(); 
     DataSetStep(count); 
    } 
}); 
} 
0

在你$.ajax()調用設置async: false

然後您也不需要您的count變量或各種if語句。

+0

只需注意:'async:false'請求將凍結瀏覽器直到服務器回覆。 – Fuhrmann

0

$(文件)。就緒(函數(){... 只在頁面加載執行,只是第一步IST執行。 嘗試在Ajax調用成功處理程序執行後續步驟

0

你可以做到這一點遞歸也:

$(function() { 
    // This is global, so do it once, outside the function 
    $.ajaxSetup ({ 
     cache: true 
    }); 

    loadingAjax('connectConferenceData','connectAttendanceData',1); 

    function loadingAjax(divId, actionId, count) { 
     $("#"+div_id).html('<img src="images/admin_uploading.gif"> Loading data...'); 
     if (count !== 6) { 
      $("#statusMsg").html('<img src="./images/admin_uploading.gif" /> Loading data for tool #'+count+' loading'); 
     } 
     $.ajax({ 
      type: "GET", 
      url: "dashboard_functions.php", 
      data: "actionID="+action_id, 
      success: function(data){ 
       $("#"+div_id).html(data).fadeIn(); 
       count++; 

       if (count === 2) { 
        loadingAjax('connectWorkshopData','cwsData',count); 
       } 
       if (count === 3) { 
        loadingAjax('relayCenterData','rcData',count); 
       } 
       if (count === 4) { 
        loadingAjax('collectionCenterData','ccData',count); 
       } 
       if (count === 5) { 
        loadingAjax('regionalStatsData','rsData',count); 
       } 
       if (count > 5) { 
        $("#statusMsg").html('All tools have been loaded.').fadeIn('slow'); 
        setTimeout(function() { 
         $('#statusMsg').fadeOut(); 
        }, 10000); 
       } 
       $("#loading").hide(); 
      } 
     });  
    } 
});