繁体   English   中英

如何仅触发一次增量

[英]how to trigger the increment only once

我已经使用了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);
    }
};

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM