如何只触发一次增量

问题描述 投票:0回答:1

我已经使用了setTimeout函数,所以我的对象停留在y<0上一段时间,那时我想我的增量只触发一次,但它继续触发...更多我使用setTimeout函数延迟我的函数增加操作的次数得到触发......所以无论我的对象在y<0停留多长时间,我的增量只触发一次的解决方案是什么

Player.prototype.checkInWater = function () {
     if (this.y < 0) {
       ++scoreGot
       setTimeout(function(){  
         player.x = 202;
         nplayer.y = 405;
       }, 300);
    }
};
javascript
1个回答
1
投票
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);
    }
};
© www.soinside.com 2019 - 2024. All rights reserved.