javascript - 如何只触发一次增量
问题描述
我已经使用了这个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);
}
};
推荐阅读
- swift - 表格视图中的自定义单元格未显示?
- android - 无法在 android studio NDK 项目中使用 OPENSSL 方法。未定义对“EVP_add_cipher”的引用
- python - 如何在不更新python中的键值的情况下迭代字典
- ruby - 如何使用 Nokogiri 和 Capybara/poltergeist 登录 Angular.io 编码的站点?
- docker - DB2 SQL 错误:SQLCODE=-204, SQLSTATE=42704 连接到 db2
- r - 无法在 R 中安装包包
- google-oauth - 如何在使用 Google 登录按钮时不将 Google 帐户添加到 Android 手机
- firebase - Flutter Cloud Firebase 设置自定义 ID(上传)
- html - 带有溢出滚动的固定位置 Div 在 Safari 中不可见
- ios - 用户登录一次后,我在设置初始化视图控制器时遇到问题