Javascript 将回调传递给对象的方法
我有一个对象构造函数,它管理我在一个网站上的很多动画,我想把一个参数传递给start函数,这个参数是一个回调,就像oncomplete是我想要实现的,因为在某些情况下,当动画停止时,我希望一些元素出现。问题是我通过了回调,但什么也没发生,我真的不知道为什么函数不接受参数,我给你留了一些代码,希望有人能帮助我Javascript 将回调传递给对象的方法,javascript,jquery,oop,constructor,callback,Javascript,Jquery,Oop,Constructor,Callback,我有一个对象构造函数,它管理我在一个网站上的很多动画,我想把一个参数传递给start函数,这个参数是一个回调,就像oncomplete是我想要实现的,因为在某些情况下,当动画停止时,我希望一些元素出现。问题是我通过了回调,但什么也没发生,我真的不知道为什么函数不接受参数,我给你留了一些代码,希望有人能帮助我 // CONSTRUCTOR WHO MANAGE THE ANIMATIONS FOR THE WEBSITE function SpriteAnimation(frame
// 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();
}
}
};
我不知道这是否是因为我没有传递上下文或类似的东西,但实际上我正在学习javascript,所以我不太了解它是如何与对象一起工作的。在我之前,每个动画都有一堆变量和函数。在你的例子中,
这个.shouldLoop
是false
。如果
var letter = new SpriteAnimation(789.695652, 18163, "letter", false, 53.3);
您的第四个参数(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();
}
}
如果我没弄错的话,这指的是窗户
else if (this.shouldLoop) { // never steps into this because this.shouldLoop is false
this.curPx = 0;
this.start();
if(callback && typeof callback === "function"){
callback();
}
}