Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

216
Views
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 answers
Answer question

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 Report

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!