gpt4 book ai didi

javascript - 为什么回调未定义?

转载 作者:行者123 更新时间:2023-11-28 20:12:13 25 4
gpt4 key购买 nike

您好,我使用对象构造函数来管理一些动画,在某些情况下,我想在动画完成时传递回调以使其他一些元素出现。问题是,如果在 if 语句中我记录了一种回调类型,它是一个有效的函数,但是当我在 if 语句内进行记录时, if 总是未定义的,如果有人可以帮助我,我很感激,我也留下了这一部分代码,以便你们可以查看。

// 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.curPx > (this.spriteWidth - this.frameWidth)){
console.log(typeof callback);
}else if(this.shouldLoop){
this.curPx = 0;
this.start();
}
};

稍后在代码中我实例化了它并运行启动:

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

letter.start(function(){
console.log("hola");
});

最佳答案

您没有在 setTimeout 中传递回调,我建议这样做:

    setTimeout(function(){
this.start(callback);
}, this.frameRate);

关于javascript - 为什么回调未定义?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19731366/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com