Javascript:增加点击次数,但每秒只增加一次

Nav*_*ngh -1 javascript counter

我想在每次单击按钮时增加一个计数器变量,但每秒只增加一次。

我已经想出增加计数器,但如果我在 1 秒内多次单击按钮,它会增加多次。

var score = 0;
increaseCount() {
  score++;
}
Run Code Online (Sandbox Code Playgroud)

我如何限制计数器每秒增加一个?

Cer*_*nce 8

当函数运行时,设置一个在 1 秒后重置的标志:

let clickAllowed = true;
let score = 0;

function increaseCount() {
  if (!clickAllowed) {
    return;
  }
  score++;
  clickAllowed = false;
  setTimeout(() => {
    clickAllowed = true;
  }, 1000);
}
Run Code Online (Sandbox Code Playgroud)

let clickAllowed = true;
let score = 0;

function increaseCount() {
  if (!clickAllowed) {
    return;
  }
  score++;
  clickAllowed = false;
  setTimeout(() => {
    clickAllowed = true;
  }, 1000);
}
Run Code Online (Sandbox Code Playgroud)
let clickAllowed = true;
let score = 0;

function increaseCount() {
  if (!clickAllowed) {
    return;
  }
  score++;
  count.textContent = score;
  clickAllowed = false;
  setTimeout(() => {
    clickAllowed = true;
  }, 1000);
}
Run Code Online (Sandbox Code Playgroud)