Timer in Javascript

Asked

Viewed 282 times

0

I wonder how I can make that, the person can only vote on a project by clicking on a button every 24 hours, half a JS Timer.

Example: "The person will vote, and only after a day he can vote again".

1 answer

0


An implementation you can do is the following, create a variable to be incremented each 1 second with the method setInterval() from the click on the button and when the variable reaches the value 86400 which is equivalent to the total of seconds in 24 hours, you use the clearInterval() to stop the counter, I don’t know if it’s the best solution to your question, but, it works:

let btn = document.getElementById('init');
let val = document.getElementById('value');
let count = 0;

btn.addEventListener('click', () => {
  btn.disabled = true;
  counter = setInterval(() => {
    Count()
  },1000)
})

function Count() {
  count++;
  console.log(count)
  if(count == 5) {
    btn.disabled = false;
    clearInterval(counter);
    count = 0;
  }
}
<button id="init">Contar</button>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.