I’m trying to make a chain reaction by executing next function after executing previous one. Code looks like this:
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')
});
}
};
This is how my object looks like. Then I run function runAnimations as a property of object. What is weird that during this chain only assetTwo function executes, but no further (assetThree). Why so?
You can’t do this type of definition:
It will call assetTwo, but it won’t have the right
thisvalue. Instead, you need to do this:Same for the other complete functions. You need to save the value of
thisinto a local variable and then use it in the complete function to invoke the next method. This will make sure thatthisis set properly for the next method.