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

113
Views
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 answers
Answer question

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 Report

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 Report

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 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!