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

118
Vistas
sound function doesn't work after the countdown finishes

I want to play a sound after my primitive pomodo app finishes. I found a function that plays a sound, added an event listener to button and it works. However, when I try to apply it to my function it doesn't. I'm just a beginnner, so I'm probably messing something up here.


        <div id="display-timer">
        <p id="w-minutes">1</p>
        <p id="colon">:</p>
        <p id="w-seconds">00</p>
      </div>

        const displayTimer = document.querySelector("#display-timer");
        const start = document.querySelector("#start-btn");
        
        function timer() {
          if (ws.innerText != 0) {
          ws.innerText--;
        } else if (wm.innerText != 0 && ws.innerText == 0) {
          ws.innerText = 59;
          wm.innerText--;
        }
        }

        function play() {
        var audio = new Audio("rooster.wav");
        audio.play();
        }

        if (wm.innerText == 0 && ws.innerText == 0) {
        play();
        }
       
about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

your js-code will be executed from top to bottom, so

if (wm.innerText == 0 && ws.innerText == 0) play();

will get executed before minutes and seconds reaches 0

as Johnny Mopp says, use setInterval to have a function executed every second and also check for your condition

example (not tested):

function timer() {
      if (ws.innerText != 0) {
      ws.innerText--;
    } else if (wm.innerText != 0 && ws.innerText == 0) {
      ws.innerText = 59;
      wm.innerText--;
    }
    if (wm.innerText == 0 && ws.innerText == 0) play();
    }

    function play() {
    var audio = new Audio("rooster.wav");
    audio.play();
    }

    window.setTimeout('timer', 1000);
about 4 years ago · Juan Pablo Isaza Denunciar

0

This should work. You should set an Interval to do that. Also, you should define ws and wm if you doesn't define in your own code.

const wm = document.querySelector('#w-minutes')
    const ws = document.querySelector('#w-seconds')
const displayTimer = document.querySelector("#display-timer");
        const start = document.querySelector("#start-btn");
        let interval;
        interval = setInterval(function(){
        if(wm.textContent == 0 && ws.textContent == 0){
            play()
             window.clearInterval(interval)
        }else if(ws.textContent == 0){
            wm.textContent = parseFloat(wm.textContent)-1;
            ws.textContent = 59
        } else{
            ws.textContent = parseFloat(ws.textContent)-1
        }
        },1000)

        function play() {
        var audio = new Audio("rooster.wav");
        audio.play();
        }
<div id="display-timer">
        <p id="w-minutes">1</p>
        <p id="colon">:</p>
        <p id="w-seconds">00</p>
      </div>

about 4 years ago · Juan Pablo Isaza Denunciar

0

Your timer function is missing a check to the play sound function.

It is not clear how your timer function gets called. Try to set an interval for every second to lower the counter by using setInterval, and save the value to clear the interval when the counter reaches 0. Then every second, check if this is the time to play the sound (if counter reached zero).

let savedTimer;

function play() {
    var audio = new Audio('rooster.wav');
    audio.play();
}

function checkIfShouldPlay() {
    if (wm.innerText == 0 && ws.innerText == 0) {
        play();
        clearInterval(savedTimer);
    }
}

function timer() {
    if (ws.innerText != 0) {
        ws.innerText--;
    } else if (wm.innerText != 0 && ws.innerText == 0) {
        ws.innerText = 59;
        wm.innerText--;
    }

    checkIfShouldPlay();
}

savedTimer = setInterval(() => timer(), 1000);

However, I think it would be better to save the timer in normal variables to run the countdown, and then reflect the value in html, instead of running the counter on innerText property of the html elements.

about 4 years ago · Juan Pablo Isaza 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