2012-03-26 62 views
1

有沒有一種方法可以告訴瀏覽器等待啓動功能,直到另一個完成?如何在單個函數中調整JavaScript事件的順序?

例如,在下面的for循環中,我希望contact()在fireForward()開始之前觸發並結束。

for ($i = 0; $i <= ($slugPace - 1); $i++) { 
    contract(); 
    moveForward(); 
    reset(); 
}; 

我試圖使用this stackoverflow thread提到$.when(// your first function).then(// your second function);,但在三家上市職能的JS功能繼續同時開火。

這裏是總的代碼,如果這能幫助:

//Contracting the slug once: 
    function contract(){ 
     $("#slug").animate({ 
      marginTop: 0, 
      width: $slugThinner, 
      height: $slugHigher, 
     }, 250); 
     $(".slugContainer").animate({ 
      marginLeft: $slugLeftMargin, 
     }, 250); 
    }; 

    //Extending the slug out and move him forward once: 
    function moveForward(){ 
     $slugLeftMargin += $slugStepsLength; 
     $("#slug").animate({ 
      marginTop: $slugMarginJS, 
      width: $slugWide, 
      height: $slugHigh, 
     }, 250); 
     $(".slugContainer").animate({ 
      marginLeft: $slugLeftMargin, 
     }, 250); 
    }; 

    function movement(){ 
     for ($i = 0; $i <= $slugStepsTotal; $i++) { 
      contract(); 
      moveForward(); 
     }; 
    } 

    function reset(){ 
     if ($slugLeftMargin > $screenWidth) { 
      $slugLeftMargin = $slugWide * -1; 
      $("#slug").stop().hide; 
      $(".slugContainer").stop.css('margin-left',$slugLeftMargin); 
      for ($i = 0; $i <= ($slugPace - 1); $i++) { 
       contract(); 
       moveForward(); 
      }; 
     } 
    } 

    //Looping the contractions to move our little friend across the screen: 
    $("#slug").click(function(){ 
     //alert('Slug width: ' + $slugWide + ', Slug height: ' + $slugHigh + ', Screen width: ' + $screenWidth + ', Slug steps total: ' + $slugStepsTotal + ', Slug higher: ' + $slugHigher + ', Slug thinner: ' + $slugThinner); 
     //alert($slugStepsTotal); 
     //alert('One step: ' + $slugStepsLength + '; Total steps: ' + $slugStepsTotal + '; Screen width: ' + $screenWidth); 
     $("#slug").show(); 
     $(".clickMe").fadeOut(300); 
     $.when(movement).then(reset()); 
    }); 

預先感謝您的幫助。

+0

我想你可以用Observer模式解決這個問題。 http://en.wikipedia.org/wiki/Observer_pattern – JSager 2012-03-26 21:33:35

回答

2

總體而言,由於您使用的animate方法,你可以做一些回調,說:

function contract(callback) { 
    $("#slug").animate({ 
      marginTop: 0, 
      width: $slugThinner, 
      height: $slugHigher, 
     }, 250); 
    $(".slugContainer").animate({ 
      marginLeft: $slugLeftMargin, 
    }, 250, function() { 
     callback(); 
     } 
    ); 

而且你會打電話合同是這樣的:

contract(function(){ moveForward(); }); 

你可以多讀一點animatehere

這就是你需要的基本概念:回調。

祝你好運!

+0

不需要使用匿名函數作爲包裝。只需傳遞函數引用即可。即,'.animate(...,250,回調)'和'合同(回調)' – gilly3 2012-03-26 21:36:16

+0

你是對的,他們沒有必要:) – Deleteman 2012-03-27 01:16:03

+0

謝謝,Deleteman。我想你只是想要釘上它。我相信我已經按照你的建議進行了設置。但是,我現在得到一個錯誤:Uncaught TypeError:undefined不是一個函數。我在while循環中做了這個唯一的語句:'contract(moveForward());'。我以這種方式開始我的功能:'函數契約(回調){'。我用'},250,callback());'結束了函數。 'moveForward'在語法上也都是正確的 - 當它不作爲回調函數使用時,它工作得很好。你能從中看到我錯過了什麼嗎? – jmealy 2012-03-29 03:48:23

相關問題