Home > Enterprise >  How do I alert when a certain value is met
How do I alert when a certain value is met

Time:04-28

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!")

CodePudding user response:

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>

CodePudding user response:

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>

CodePudding user response:

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)

  • Related