2011-10-24 4 views
1

다른 함수가 완료 될 때까지 함수 실행을 중지하려면 어떻게합니까?jQuery - 진행하기 전에 함수 실행

내가 있는지 확인하려면
$('#div').load('sub.html', function() { 

    //Do this once done loading the sub.html file 
    setTimeout(function() { 

     //Run SomeFunction(); before doing anyhing else 
     $('#tabsWrapper').removeClass('ajaxLoading'); 
     $('#tabsWrapper #loaderDiv').fadeIn(500); 

    } , 1000); 

}); 

SomeFunction()는 .. $('#tabsWrapper').removeClass('ajaxLoading'); 등을 수행하기 전에 수행

어떤 제안 내가 그걸 어떻게 달성 할 수 있습니까? 어떤 도움을 많이 주셨습니다.

+0

반환 값을 참조하고 당신이 일을 끝낼 경우, 당신은 값까지 루프가 반환 할 수 참조하십시오. – Dev

+0

은 .load에서 콜백을 사용합니다. http://stackoverflow.com/questions/6688958/jquery-load-callback-scrolling-css-overflow, 그 작동합니다 믿습니다. – Matt

+1

스크립트의 정상적인 흐름 인 것 같습니다. simeFunction은'ajax'와 같은 것을 가지고 있지 않습니다. 당신이 그것을 필요로하는 방식으로 실행합니다. – Varun

답변

1

반환 일부 당신의 SomeFunction()에서 값이 같은 조건에 넣어 :

더 나은 방법이 같은 자기 호출하는 익명 함수 뭔가를 사용하는 것입니다
var result = null; // important to put it here eg before setTimeout 

//Do this once done loading the sub.html file 
setTimeout(function() { 

    result = SomeFunction(); 

    if (result === 'whatever that function returns') { 
     $('#tabsWrapper').removeClass('ajaxLoading'); 
     $('#tabsWrapper #loaderDiv').fadeIn(500); 
    } 

} , 1000); 

:

(function foo(){ 
    doStuff; 

    setTimeout(foo, 500); 

})() 

Here setTimeout will only trigger when doStuff is finished.

+0

quastion은 -'SomeFunction()'이'if statement' 전에 끝나지 않으면 건너 뛸 수 있습니까? 실행 순서가 유지되고 있는지 확인하고 싶습니다. – Iladarsda

+0

@ 새 사용자 : 나중에 접근 방법을 참조하십시오. – Sarfraz

+0

거의 다 있습니다. 'doStuff();'가 끝나면 어디에서 코드를 실행할 것인가? 'setTimeout' 전후에, 아니면 안에 있습니까? 그러나 어떻게? :) – Iladarsda

2
$('#div').load('sub.html', function(response, status) { 

    if(status=="success"){ 
      //do your stuff here ..its loaded 
    } 

}); 
2

콜백 메소드를 사용하십시오.

된 SomeFuncion에서 JQuery와 API

관련 문제