Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

133
Visualizações
A task with displaying clicks on a given button (Javascript)

Make a button that displays the number of clicks on it. If the next click occurs faster than a second after the previous click on the button, the button is blocked (use the disabled attribute), the click counter is reset to zero.

My attempt:

const buttons = document.getElementById(`button`);

buttons.onclick = function () {
  buttons.disabled = true;
  setTimeout(function () { buttons.disabled = false }, 1000);
  console.log(`click`)
}

buttons.addEventListener('click', () => {
  console.log(`Bbb`);
  let clicks = +this.dataset.clicks;
  clicks += 1;
  console.log(clicks);
  this.dataset.clicks = clicks;
});

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

const btn = document.getElementById('button');
let clicks = 0;

btn.addEventListener('click', function () {
  clicks += 1;
  console.log('click');
  this.innerHTML = clicks;
});

I discourage posting mere solutions for coding exercises, but I don't really know how to explain this, lol.

Is this the behavior you want to achieve?

about 4 years ago · Juan Pablo Isaza Relatório

0

Using a helper function that returns current time in ms, we just store every click the time, and compare it to last one.

function now() {
  return (new Date()).getTime();
}

const button = document.getElementById(`button`);


button.addEventListener('click', function() {
  if (this.last && now() - (this.last) < 1000) {
    this.innerText = "0"
    this.disabled = true;
    return;
  }
  let clicks = parseInt(this.innerText, 10);
  clicks += 1;
  this.innerText = clicks;
  this.last = now();
});
button {
  width: 200px;
  height: 40px;
}
<button id="button">0</button>

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda