我試圖通過執行上一個函數後執行下一個函數來產生連鎖反應。代碼如下所示:上次完成時無法觸發下一個函數
var med = {
imgLoadTime : 2000,
easingEffect : 'easeOutQuart',
scrollEase : 'easeInOutQuad',
effectDuration : 1000,
currentPage : '',
runAnimations : function(){
if(this.currentPage == '#slide5'){
this.initAssets();
}
},
initAssets : function(){
$('#asset-1').animate(
{left : '50%'},
{
duration: this.effectDuration,
easing: this.easingEffect,
complete: this.assetTwo
});
},
assetTwo : function(){
console.log('two');
debugger;
$('#asset-2').animate(
{left : '50%'},
{
duration: this.effectDuration,
easing: this.easingEffect,
complete: this.assetThree
});
},
assetThree : function(){
console.log('three');
$('#asset-3').animate(
{left : '50%'},
{
duration: this.effectDuration,
easing: this.easingEffect,
complete: console.log('weszlo')
});
}
};
這就是我的對象的外觀。然後我運行函數runAnimations作爲對象的一個屬性。奇怪的是,在這個鏈中只有asset兩個函數執行,但沒有進一步(assetThree)。爲什麼這樣?
+1 - 擊敗我吧。另一個'self = this'的例子! :)'self = this'的原因是因爲OP認爲他們的目標是'this'的父母仍然保留子函數中'self'的範圍。 –
謝謝!再次是範圍問題。 ech,可愛的js;) – lukaleli
@ lesny09 - this'的值由調用者設置。在動畫完整函數中,'this'可能被設置爲正在動畫的對象。因此,在你的原始代碼發生完成並且調用了assetTwo()時,'this'不會被設置爲你的對象,而是被設置爲動畫的#asset-1。這會弄亂動畫中的下一個項目,因爲'this.assetThree()'不會存在,因爲它是錯誤的'this'。由於它不存在,它會停止序列。將正確的值保存到局部變量中,並使用該變量保證它獲得正確的「this」。 – jfriend00