我用过setTimeout
函数使我的对象保持在 y<0
有一段时间,当时我希望我的增量只触发一次,但它不断触发...更多我使用 setTimeout
延迟我的函数函数更高的次数增量操作被触发……那么有什么解决方案可以让我的增量只触发一次,无论我的对象在 y<0
停留多久
Player.prototype.checkInWater = function () {
if (this.y < 0) {
++scoreGot
setTimeout(function(){
player.x = 202;
nplayer.y = 405;
}, 300);
}
};
最佳答案
Player = function(){
....
this.checkedInWater = false;
}
Player.prototype.checkInWater = function () {
if (this.y < 0 && !this.checkedInWater) {
++scoreGot;
t = this;
t.checkedInWater = true;
setTimeout(function(){
player.x = 202;
nplayer.y = 405;
t.checkedInWater = false;
}, 300);
}
};
关于javascript - 如何只触发一次增量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52113928/