用jQuery一次加载一个函数?
我有一个要一次加载一个的函数列表。我无法让他们加载一个,然后在加载数据后再转到下一个。这是我的密码:用jQuery一次加载一个函数?,jquery,while-loop,Jquery,While Loop,我有一个要一次加载一个的函数列表。我无法让他们加载一个,然后在加载数据后再转到下一个。这是我的密码: $(document).ready(function() { //step through the data sets var count = 1; if (count = 1) { loadingAjax('connectConferenceData','connectAttendanceData',count); } if (count
$(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();
}
});
}
$(文档).ready(函数(){
//逐步浏览数据集
var计数=1;
如果(计数=1){
加载JAX('connectConferenceData','connectAttendanceData',计数);
}
如果(计数=2){
加载jax('connectWorkshopData','cwsData',count);
}
如果(计数=3){
加载jax('relayCenterData','rcData',count);
}
如果(计数=4){
加载JAX('collectionCenterData','ccData',count);
}
如果(计数=5){
加载jax('regionalStatsData','rsData',count);
}
如果(计数>5){
$(“#statusMsg”).html('所有工具都已加载').fadeIn('慢');
setTimeout(函数(){
$('#statusMsg').fadeOut();
}, 10000 );
}
});
//函数获取数据集
函数装入jax(div_id,action_id,count){
$(“#加载”).show();
$(“#”+div_id).html('Loading data…');
$.ajaxSetup({
缓存:真
});
$.ajax({
键入:“获取”,
url:“dashboard_functions.php”,
数据:“actionID=“+action\u id,
成功:函数(数据){
$(“#”+div_id).html(data.fadeIn();
计数++;
如果(计数!=6){
$(“#statusMsg”).html('loadingdatafortool#'+count+'Loading');
}
$(“#加载”).hide();
}
});
}
在您的$.ajax()中调用集async:false
然后,您也不需要您的count
变量或各种if
语句。$(文档).ready(函数(){。。。
仅在页面加载时执行,并且仅执行第一步。
尝试在ajax调用的成功处理程序中执行后续步骤您可以设置如下一系列回调:
//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);
}
});
}
//逐步浏览数据集
函数数据集步骤(计数){
如果(计数=1){
加载JAX('connectConferenceData','connectAttendanceData',计数);
}
如果(计数=2){
加载jax('connectWorkshopData','cwsData',count);
}
如果(计数=3){
加载jax('relayCenterData','rcData',count);
}
如果(计数=4){
加载JAX('collectionCenterData','ccData',count);
}
如果(计数=5){
加载jax('regionalStatsData','rsData',count);
}
如果(计数>5){
$(“#statusMsg”).html('所有工具都已加载').fadeIn('慢');
setTimeout(函数(){
$('#statusMsg').fadeOut();
}, 10000 );
}
}
$(文档).ready(函数(){
数据集步骤(1);
});
//函数获取数据集
函数装入jax(div_id,action_id,count){
$(“#加载”).show();
$(“#”+div_id).html('Loading data…');
$.ajaxSetup({
缓存:真
});
$.ajax({
键入:“获取”,
url:“dashboard_functions.php”,
数据:“actionID=“+action\u id,
成功:函数(数据){
$(“#”+div_id).html(data.fadeIn();
计数++;
如果(计数!=6){
$(“#statusMsg”).html('loadingdatafortool#'+count+'Loading');
}
$(“#加载”).hide();
数据集步骤(计数);
}
});
}
您也可以递归执行:
$(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();
}
});
}
});
$(函数(){
//这是全局性的,所以在函数外部执行一次
$.ajaxSetup({
缓存:真
});
加载JAX('connectConferenceData','connectAttendanceData',1);
函数loadingAjax(divId、actionId、count){
$(“#”+div_id).html('Loading data…');
如果(计数!==6){
$(“#statusMsg”).html('loadingdatafortool#'+count+'Loading');
}
$.ajax({
键入:“获取”,
url:“dashboard_functions.php”,
数据:“actionID=“+action\u id,
成功:函数(数据){
$(“#”+div_id).html(data.fadeIn();
计数++;
如果(计数==2){
加载jax('connectWorkshopData','cwsData',count);
}
如果(计数==3){
加载jax('relayCenterData','rcData',count);
}
如果(计数=4){
加载JAX('collectionCenterData','ccData',count);
}
如果(计数=5){
加载jax('regionalStatsData','rsData',count);
}
如果(计数>5){
$(“#statusMsg”).html('所有工具都已加载').fadeIn('慢');
setTimeout(函数(){
$('#statusMsg').fadeOut();
}, 10000 );
}
$(“#加载”).hide();
}
});
}
});
第一次查看时,您需要在所有if语句中使用“==”而不是“=”,或者甚至===来强制不进行类型强制,或者只是删除它并成功地进行正确的调用handler只需注意:async:false
请求将冻结浏览器,直到服务器响应。