0

I've came across with an interesting event.

I'm trying to build a pomodoro timer that runs in the background with an alarm that fires when the timer finishes and starts.

I coded a WebWorker Script that runs that same timer.

worker.js

self.addEventListener("message", (e) => {
    if (e.data.task === 'run the timer') {
        currentTimeLeftInSession = e.data.defaultWorkTime;

        countdown = setInterval(() => {
            currentTimeLeftInSession--;
            currentTimeLeftInSessionBackwards++;
            if(currentTimeLeftInSession < 0) {
                return;
            }
            let timeLeft = getTimeLeft(currentTimeLeftInSession);
            let data = {
                'timeLeft': timeLeft,
            }
            self.postMessage(data);
        }, 1000);
    }

function getTimeLeft(seconds) {
    let minutes = Math.floor(seconds / 60);
    let remainderSeconds = Math.floor(seconds % 60);
    let timeLeft = `${minutes}:${remainderSeconds}`;

    if (remainderSeconds < 10) {
        timeLeft = `${minutes}:0${remainderSeconds}`;
        if (minutes < 10) {
            timeLeft = `0${minutes}:0${remainderSeconds}`;
        }
    }
    return timeLeft;
}
})

And added an event listener to the pomodoro script that updates the timer display

pomodoro.js

let worker = new Worker('/lib/js/worker.js')
worker.addEventListener('message', (e) => {
        progressBar.text.innerText = e.data.timeLeft;
        console.log(e.data.timeLeft)
        if(e.data.timeLeft == '00:00') {
            playAudio(audioNumber);
        }

    });

 startButton.addEventListener('click', () => {
        defaultWorkTime = 200
        let data = {
            'task': 'run the timer',
            'defaultWorkTime': defaultWorkTime,
            'defaultBreakTime': defaultBreakTime,
        }
        worker.postMessage(data);
    });

The interesting thing here:

If i remove the console.log the timer stops getting updated. If i set it all goes with the plan.

Why does this happen???

Is there a way that the timer doesn't stop after couple of seconds if it's running on background?

Odas0R
  • 11
  • 1
  • 4
  • Most of your assertions here are not correct. That isn't how a CPU works, also ..........tickle – Libra Jan 09 '20 at 16:59
  • Hahahah! Okay, you got me there. Thank you! Ohhh well i had an idea of how it works with the cpu, i guess it's different on the web :( – Odas0R Jan 09 '20 at 17:04
  • 1
    [How do browsers pause/change Javascript when tab or window is not active?](https://stackoverflow.com/questions/15871942/how-do-browsers-pause-change-javascript-when-tab-or-window-is-not-active) – Andreas Jan 09 '20 at 17:05
  • How is `getTimeLeft` implemented? And how are you starting the worker? –  Jan 09 '20 at 17:05
  • It's just a conversion seconds to a display like XX:YY , X-> Minutes, Y-> Seconds Added that to the question! Thank you – Odas0R Jan 09 '20 at 17:08
  • @Andreas Thank you, makes a lot of sense! – Odas0R Jan 09 '20 at 17:39

0 Answers0