2013-06-17 2 views
-1

이 익명 함수 정의 체인을 별도의 함수로 다시 작성하여보다 관리하기 쉽고 읽을 수있게 만들 수 있습니까? 감사!setTimeout()으로 콜백을 중첩합니다.

function animation(){ 

    var timeout; 
    timeout=timeoutsetTimeout(function(){ 
     console.log('step1') 

     timeout=setTimeout(function(){ 
      console.log('step2') 

      timeout=setTimeout(function(){         
       console.log('almost there') 

       setTimeout(function(){ 

         console.log('grand finale') 

        }, 300); 

      }, 1000); 


     }, 2000); 


    }, 5300); 
} 
+0

이것은 스택 오버플로 오프 주제입니다. http://codereview.stackexchange.com을 시도하십시오 – meagar

+0

jquery 약속을 찾으십시오. –

답변

1

솜씨, 당신은 JS 대신 IOC 또는 DI의 폐쇄를 사용할 수 있습니다

function animation(){ 

    var timeout; 

    function one(){ 
     console.log('step1'); 
     timeout=setTimeout(two, 2000); 
    } 

    function two(){ 
     console.log('step2'); 
     timeout=setTimeout(three, 1000); 
    } 

    function three(){ 
     console.log('almost there'); 
     timeout=setTimeout(four, 300); 
    } 

    function four(){ 
     timeout=console.log('grand finale'); 
    } 

    return timeout=setTimeout(one, 5300); 

} 
관련 문제