简体   繁体   English

以前完成时无法触发下一个功能

[英]Cannot trigger next function when previous completed

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. 然后我运行函数runAnimations作为对象的属性。 What is weird that during this chain only assetTwo function executes, but no further (assetThree). 奇怪的是,在这个链中只有assetTwo函数执行,但没有进一步(assetThree)。 Why so? 为什么这样?

You can't do this type of definition: 你不能做这种类型的定义:

complete: this.assetTwo 

It will call assetTwo, but it won't have the right this value. 它会调用assetTwo,但不会有正确的this值。 Instead, you need to do this: 相反,你需要这样做:

           initAssets : function(){
                var self = this;
                $('#asset-1').animate(
                    {left : '50%'}, 
                    { 
                        duration: this.effectDuration, 
                        easing: this.easingEffect, 
                        complete: function() {self.assetTwo()}
                    });
            },

Same for the other complete functions. 其他完整功能也是如此。 You need to save the value of this into a local variable and then use it in the complete function to invoke the next method. 您需要将this的值保存到局部变量中,然后在complete函数中使用它来调用下一个方法。 This will make sure that this is set properly for the next method. 这将确保this设置正确的一个方法。

Your this changes with each function, you could reference it by med instead to get the desired result: 您的每个函数都会更改,您可以通过med引用它来获得所需的结果:

assetTwo : function(){

                //debugger;
                $('#asset-2').animate(
                    {left : '50%'}, 
                    { 
                        duration: med.effectDuration, 
                        easing: med.easingEffect, 
                        complete: med.assetThree 
                    });
            },

Updated fiddle: http://jsfiddle.net/johnkoer/2KHnc/16/ 更新小提琴: http//jsfiddle.net/johnkoer/2KHnc/16/

在你的javascript代码中的所有地方使用med而不是this

For nice tight code use a jQuery $.Deferred.pipe() chain as described here 对于漂亮的紧身代码使用jQuery的$.Deferred.pipe()所描述的链条在这里

You should end up with something like this : 你应该得到这样的东西:

var med = {
    imgLoadTime: 2000,
    currentPage: '',
    css : {left: '50%'},
    animOptions: {
        duration: 1000, 
        easing: 'easeOutQuart'
    };
    runAnimations: function() {
        if(med.currentPage == '#slide5') {
            $.Deferred(function(dfr) {
                dfr.pipe(function() {
                    return $('#asset-1').animate( med.css, med.animOptions );
                }).pipe(function() {
                    return $('#asset-2').animate( med.css, med.animOptions );
                }).pipe(function() {
                    return $('#asset-3').animate( med.css, med.animOptions );
                })
            }).resolve();
        }
    }
};

untested 未经测试

Get the hang of Deferreds and you'll never look back. 掌握Deferreds,你永远不会回头。

Unless the med object is important for other reasons, then it would be simpler just to have runAnimations() rather than an object wrapper : 除非med对象由于其他原因很重要,否则只有runAnimations()而不是对象包装器会更简单:

function runAnimations() {
    var imgLoadTime = 2000,
    currentPage = '',
    css = {left: '50%'},
    animOptions = {
        duration: 1000, 
        easing: 'easeOutQuart'
    };
    if(currentPage == '#slide5') {
            $.Deferred(function(dfr) {
                dfr.pipe(function() {
                    return $('#asset-1').animate( css, animOptions );
                }).pipe(function() {
                    return $('#asset-2').animate( css, animOptions );
                }).pipe(function() {
                    return $('#asset-3').animate( css, animOptions );
                })
            }).resolve();
    }
}

This way references to the fixed parameters are straightforward. 这种方式对固定参数的引用很简单。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM