如何使用 Kedown 一次运行 1 次函数?还有这个符号是什么意思? "!"


$(document).keydown(function () {
if (!started)
nextSequence();
})
$(document).keypress(function() {
if (!started) {
$("#level-title").text("Level " + level);
nextSequence();
started = true;
}
});

对于按下键,更改标题:
h1标题开头是"press A Key to start",当游戏开始时,将其更改为"Level 0"。

你的问题有点令人困惑。感叹号(!)表示"不是";(在这种情况下{if "not"Started},即if Started == false)。但我不确定你想问的是一次运行一个函数。警告一句,如果你问的问题可能在网上其他地方可以找到答案,这里的人真的会对你怒不可遏,所以在你发帖之前,至少应该在谷歌上搜索一下。如果你能把问题的第一部分改写得更清楚一点,你可能会得到一些答案,但我建议你把问(!)符号的部分删掉。

我希望你有一个愉快的一天!

if (e.repeat) return;添加到函数的开头

$(document).keydown(function(e) {
if (e.repeat) return;
//3. The h1 title starts out saying "Press A Key to Start", ...
$("#level-title").text("Level " + level);
nextSequence();
started = true;
});

感叹号(" ! ")符号,称为" bang ",是逻辑上的" not "操作符。

如果started变量在按下键之前初始化为false,那么(!started)将计算为true,并且该块中的代码将运行。当按下键并运行代码时,started将被设置为true,因此,如果发生额外的按键,(!started)将计算为false,并且代码将不会再次运行。

最新更新