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

182
Views
how to cancel pop up audio in javascript

hello I'm new to code and javascript I'm creating a clock with timer. the countdown is done well with an alarm and a pop up when the timer reaches zero. only I can't turn off the alarm when I close the pop up it opens in a loop. i need help please

let departInput = document.querySelector('#times');
let subButton = document.querySelector('#sub')


subButton.addEventListener('click', event => {
  let temps = (departInput.value)*60;
  var timerElement = document.getElementById("MyTimerDisplay")
  setInterval(() => {
  let minutes = parseInt(temps / 60, 10)
  let secondes = parseInt(temps % 60, 10)

  minutes = minutes < 10 ? "0" + minutes : minutes
  secondes = secondes < 10 ? "0" + secondes : secondes

  timerElement.innerText = `${minutes}:${secondes}`
  temps = temps <= 0 ? 0 : temps - 1
  if(minutes == 0 && secondes == 0){
    var snd = new Audio('clock.mp3');
    snd.play();
    alert("cest l'heure")

    }
  }, 1000)

});
  
about 4 years ago · Santiago Trujillo
1 answers
Answer question

0

To address the problem you are having you should call clearTimer() when the alarm rings so that its callback won't be running again and again each second even after the condition to break was met.

Here's a working example to show the point:

let departInput = document.querySelector('#times');
let subButton = document.querySelector('#sub')

subButton.addEventListener('click', event => {

  //decides how many minutes before the alarm rings
  let minutesToAlarm = parseInt(departInput.value);
  //initializes elapsedSeconds
  let elapsedSeconds = minutesToAlarm*60;
  
  var timerElement = document.getElementById("MyTimerDisplay");
  
  var myTimer = setInterval(() => {
  
    //converts elapsedSeconds in mm:ss format and refresh the ui
    let minutes = Math.floor(elapsedSeconds/60);
    let secondes = elapsedSeconds % 60;
    timerElement.innerText = `${minutes}:${secondes}`
    
    //decrement elapsedSeconds by 1 
    elapsedSeconds--;
    
    if(elapsedSeconds === 0){
      /*
       * Part muted because there's no reference to clock.mp3
       *
      var snd = new Audio('clock.mp3');
      snd.play();
      */
      alert("cest l'heure");

      //here clears the timer so that the callback
      //won't be running again anymore
      clearTimeout(myTimer);
    }
  }
  , 1000);
});
      
#sub{
  border: solid 1px black;
  min-width: 5em;
  padding: 2px;
  cursor: pointer;
}

#MyTimerDisplay{
  border: dashed 4px darkgray;
  margin-top: 5px;
  width: fit-content;
  padding: 5px 20px;
}
<input id="times" type="text" placeholder="departInput"/>
<button id="sub">clickme</button>

<div id="MyTimerDisplay">#times</div>

about 4 years ago · Santiago Trujillo 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!