2011-03-04 16 views
2

我想調用一些函數,但等待前一個已完成。 我知道jQuery在幾個函數中提供了一個回調參數,但我想了解如何在我自己的jQuery插件中實現此行爲。所以這樣的話:我怎樣才能調用一些javascript函數,但是,等待之前已經完成了?

my previous question讀答案後,我寫了這個:

(function(callback){ 
     $('#art1').animate({'width':'1000px'},1000); 
     callback(); 
    })((function(callback2){ 
     $('#art2').animate({'width':'1000px'},1000); 
     callback2(); 
    })(function(){ 
     $('#art3').animate({'width':'1000px'},1000); 
    })); 

但不工作。我再次嘗試,並寫道:

animate1(function(){ 
     animate2(function(){ 
      animate3(); 
     }) 
    }); 

    function animate1(callback){ 
     $('#art1').animate({'width':'1000px'},1000); 
     callback(); 
    } 
    function animate2(callback){ 
     $('#art2').animate({'width':'1000px'},1000); 
     callback(); 
    } 
    function animate3(callback){ 
     $('#art3').animate({'width':'1000px'},1000); 
     callback(); 
    } 

但仍然無法正常工作。三個動畫仍然在同一時間開始。我希望他們被稱爲一個接一個。但是,如果沒有使用:

$('#art1').animate({'width':'1000px'},1000,'linear',function(){ 
     $('#art2').animate({'width':'1000px'},1000,'linear',function(){ 
      $('#art3').animate({'width':'1000px'},1000);   
     });   
    }); 
+0

你有沒有考慮過使用'$ .delay()'?不知道它是否會做到你想要的。否則,我認爲將每個動畫作爲回調傳遞給該動畫是它的前進方向。 – 2011-03-04 15:23:09

+0

@Brian Driscoll:我不知道需要多少時間。我不能使用$ .delay() – texai 2011-03-04 15:30:04

回答

3

這是什麼Buffer是:

var Buffer = function(handler) { 
    var tasks = []; 
    // empty resolved deferred object 
    var deferred = $.when(); 

    // handle the next object 
    function handleNextTask() { 
     // if the current deferred task has resolved and there are more tasks 
     if (deferred.isResolved() && tasks.length > 0) { 
      // grab a task 
      var task = tasks.shift(); 
      // set the deferred to be deferred returned from the handler 
      deferred = handler(task); 
      // if its not a deferred object then set it to be an empty deferred object 
      if (!(deferred && deferred.promise)) { 
       deferred = $.when(); 
      } 
      // if we have tasks left then handle the next one when the current one 
      // is done. 
      if (tasks.length > 0) { 
       deferred.done(handleNextTask); 
      } 
     } 
    } 

    // appends a task. 
    this.append = function(task) { 
     // add to the array 
     tasks.push(task); 
     // handle the next task 
     handleNextTask(); 
    }; 
}; 

然後我們剛剛創建動畫,動畫任務的任務處理程序和任務追加到緩衝區。

function handleAnimation(task) { 
    var def = $.Deferred(); 
    $(task.id).animate(task.props, task.timeout, task.options function() { 
     def.resolve(); 
    }); 
    return def.promise(); 
} 

function AnimationTask(id, props, timeout, options) { 
    this.id = id; 
    this.props = props; 
    this.timeout = timeout; 
    this.options = options; 
} 

var buffer = new Buffer(handleAnimation); 
buffer.append(new AnimationTask("#art1", { 
    "width": "1000px" 
}, 1000, "linear"); 
buffer.append(new AnimationTask("#art2", { 
    "width": "1000px" 
}, 1000, "linear"); 
buffer.append(new AnimationTask("#art3", { 
    "width": "1000px" 
}, 1000, "linear"); 

這使用jQuery的魔術推遲執行一個接一個的任務。將它們鏈接起來可能更容易。

而且這會工作:

(function(callback){ 
    $('#art1').animate({'width':'1000px'},1000, function() { 
     callback(); 
    }); 
})((function(callback2){ 
    $('#art2').animate({'width':'1000px'},1000, function() { 
     callback2(); 
    }); 
})(function(){ 
    $('#art3').animate({'width':'1000px'},1000); 
})); 
0

您需要將您的回調作爲參數傳遞給調用動畫:

function Animate(afterFirst,afterSecond) { 
    $('#art1').animate({'width':'1000px'},1000,function(afterFirst,afterSecond) { 
    afterFirst(); 
    $('#art2').animate({'width':'1000px'},1000,function(afterSecond) { 
     afterSecond(); 
     $('#art3').animate({'width':'1000px'},1000); 
    }); 
    }); 
} 
+0

我不想使用動畫函數的回調參數。我想學習如何編寫一個支持回調的函數作爲參數,以實現一系列函數調用。 – texai 2011-03-04 15:34:13

+0

@texai您需要在某個點使用'callback'參數,否則您無法知道動畫何時完成。 – Raynos 2011-03-04 15:38:06

+0

我發佈後看到了。 – Jordan 2011-03-04 15:38:41

0

如果你只是想讓你的動畫一一運行,你只需要設置queue:true作爲其中一個動畫屬性。

+0

我知道。正如你可以閱讀我的問題,我真正想學的是如何在我自己的jQuery插件中實現這種行爲。 – texai 2011-03-04 15:32:10

+0

@texai看''.queue' – Raynos 2011-03-04 15:41:19

相關問題