Home > Mobile >  sound function doesn't work after the countdown finishes
sound function doesn't work after the countdown finishes

Time:12-30

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();
        }
       

CodePudding user response:

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.

CodePudding user response:

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);

CodePudding user response:

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>

  • Related