2011-05-09 3 views

답변

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 
}); 

이 멋지게 몇 가지 작은 수정을 두 개 이상의 목록에 확장 :

  • 사용하는 대신 one_donevar how_many_done = 0;.
  • one_done = true; 대신 ++how_many_done;을 수행하고 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); 
+0

멋진데, 남자! – Cynial

+0

@Cynial : 여분의 상태를 격리하는 또 다른 업데이트를 추가했습니다. –

+0

그런 식으로 말하지 마세요. 정말 도움이됩니다! :) – Cynial

관련 문제