Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

116
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda