1

Web サイトにある多くのアニメーションを管理するオブジェクト コンストラクターがあります。パラメーターを start 関数に渡したいと思います。このパラメーターはコールバックです。oncomplete のように、達成したいのはアニメーション停止いくつかの要素を表示したい。問題は、コールバックを渡すことですが、何も起こらず、関数がパラメーターを取得しない理由が本当にわかりません。誰かが私を助けてくれることを願って、いくつかのコードを残します。

// CONSTRUCTOR WHO MANAGE THE ANIMATIONS FOR THE WEBSITE
        function SpriteAnimation(frameWidth, spriteWidth, spriteElement, shouldLoop, frameRate){
            this.frameWidth = frameWidth;
            this.spriteWidth = spriteWidth;
            this.selector = document.getElementById(spriteElement);
            this.shouldLoop = shouldLoop ;
            this.curPx = 0;
            this.frameRate = frameRate;
        }

        SpriteAnimation.prototype.start = function(callback){

            this.selector.style.backgroundPosition = "-" + this.curPx + "px 0px";
            this.curPx += this.frameWidth;

            if (this.curPx < (this.spriteWidth - this.frameWidth)){
                setTimeout(this.start.bind(this), this.frameRate);
            } else if (this.shouldLoop) {
                this.curPx = 0;
                this.start();

                if(callback && typeof callback === "function"){
                    callback();
                }
            }

        }; 

今、コールバックを呼び出すために使用しているコードの一部:

var letter = new SpriteAnimation(789.695652, 18163, "letter", false, 53.3);

letter.start(function(){
    $("#form-field").fadeIn();
});

おそらくコンテキストを渡していないのか、そのようなものなのかはわかりませんが、実際にはJavaScriptを学んでいるので、オブジェクトでどのように機能するのかよくわかりません。以前は、アニメーションごとにたくさんの変数と関数がありました。

4

1 に答える 1

2

あなたの場合、this.shouldLoopですfalse。それは決して入りませんelse if

var letter = new SpriteAnimation(789.695652, 18163, "letter", false, 53.3);

4 番目のパラメーター (false) が割り当てられますshouldLoop

else if (this.shouldLoop) { // never steps into this because this.shouldLoop is false
                this.curPx = 0;
                this.start();

                if(callback && typeof callback === "function"){
                    callback();
                }
            }
于 2013-11-01T14:51:49.423 に答える