$(document).ready(function() {
$("#list1").jqGrid({
url: 'example1.php',
/*balabala...*/
gridComplete: function() {
}
});
$("#list2").jqGrid({
url: 'example2.php',
/*balabala...*/
gridComplete: function() {
}
});
/*I want to do something here, but the above grids must be both complete first.*/
Something...
});
我该怎么办?谢谢!
答案 0 :(得分:3)
最简单的方法是将你的“东西”放在gridComplete
回调中,但让两个回调检查另一个回调完成。这些方面的东西:
function do_something_wonderful() {
// This is the awesome stuff that you want to
// execute when both lists have loaded and finished.
// ...
}
var one_done = false;
function done_checker() {
if(one_done) {
// The other one is done so we can get on with it.
do_something_wonderful();
}
one_done = true;
}
$("#list1").jqGrid({
//blah blah blah
gridComplete: done_checker
});
$("#list2").jqGrid({
//blah blah blah
gridComplete: done_checker
});
这很好地扩展到两个以上的列表,只有几个小的修改:
var how_many_done = 0;
代替one_done
。++how_many_done;
代替one_done = true;
并将其移至done_checker
的顶部。if(one_done)
替换为if(how_many_done == number_of_tasks)
,其中number_of_tasks
是您拥有的AJAX任务数量。通用版本看起来像这样:
var number_of_tasks = 11; // Or how many you really have.
var how_many_done = 0;
function done_checker() {
++how_many_done;
if(how_many_done == number_of_tasks) {
// All the AJAX tasks have finished so we can get on with it.
do_something_wonderful();
}
}
更好的版本会在闭包中结束状态:
var done_checker = (function(number_of_tasks, run_when_all_done) {
var how_many_done = 0;
return function() {
++how_many_done;
if(how_many_done == number_of_tasks) {
// All the AJAX tasks have finished so we can get on with it.
run_when_all_done();
}
}
})(do_something_wonderful, 11);
答案 1 :(得分:0)
您当前应该使用jquery deferred objects。