Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

180
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda