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

218
Visualizações
How do I alert when a certain value is met

So I'm making a timer in html and javascript and I am displaying it on a button. I want to make it so that when the value of time is equal to zero, it alerts the user. Here is my code so far. btw, the code is part of the timer code.

      const time2=
  document.getElementById("countdown")
if(time2==0:00)
  alert("You have ran out of time, better luck next time!")
about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

something like this?

let div = document.getElementById('count')
let countdown = 10
let stopper = setInterval(myTimer, 1000)

function myTimer() {
  countdown--
  div.innerHTML = countdown + " seconds left"

  if (countdown == 0) {
    clearInterval(stopper)
  }
}
<div id='count'>10 seconds left</div>

about 4 years ago · Juan Pablo Isaza Relatório

0

You can create a timer with a setInterval() and trigger some functionality when a value is reached, in this case the timer is cleared, i hope this helps:

const createTimer = (num) => {
    const timer = setInterval(() => {
        console.log(num)
        num--;
        if(num == 0) {
            console.log("You have ran out of time, better luck next time!")
            clearInterval(timer)
        }
    }, 1000)
}
createTimer(4)

about 4 years ago · Juan Pablo Isaza Relatório

0

Here is one possible implementation of a very simple Timer using a class. Most important is setInterval() which will allow you to run a function every x milliseconds.

This timer uses a callback function onUpdate(seconds) which is called every time the timer is updated and provides the remaining seconds as a parameter. You could certainly extend on that.

It is also required to wait for the HMTL document to have loaded before trying to access the DOM using the DOMContentLoaded event.

class Timer {
  #interval;
  #initial;
  #seconds;

  constructor(seconds, onUpdate) {
    this.#initial = seconds;
    this.#seconds = seconds;
    this.onUpdate = onUpdate;
    this.onUpdate(seconds);
  }

  start() {
    this.#interval = setInterval(() => {
      this.#seconds--;
      if (this.#seconds === 0) this.stop();
      this.onUpdate(this.#seconds);
    }, 1000);
  }

  reset() {
    this.stop();
    this.#seconds = this.#initial;
    this.onUpdate(this.#seconds);
  }

  stop() {
    clearInterval(this.#interval);
    this.onUpdate(this.#seconds);
  }
}

// wait for HTML document to load
window.addEventListener("DOMContentLoaded", e => {
  const startBtn = document.getElementById("start");
  const stopBtn = document.getElementById("stop");
  const resetBtn = document.getElementById("reset");

  // create a timer and assign it seconds as well as a callback function that is called every second
  const timerDiv = document.getElementById("countdown");
  const timer = new Timer(5, (seconds) => {
    // called every seconds with seconds to go as an argument
    timerDiv.textContent = `${seconds}s to go`
  })

  // add event listeners to buttons
  startBtn.addEventListener("click", () => timer.start());
  stopBtn.addEventListener("click", () => timer.stop());
  resetBtn.addEventListener("click", () => timer.reset());
})
<div id="countdown"></div>
<button id="start">Start</button>
<button id="stop">Stop</button>
<button id="reset">Reset</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