devv-work / timeato

Track productivity, allot time for focusing on tasks, and increase your effectiveness during study sessions
https://timeato.up.railway.app/
7 stars 1 forks source link

Remove delay on starting timer #108

Closed Brandon-Schefstad closed 1 year ago

Brandon-Schefstad commented 1 year ago

setInterval() automatically imposes a delay on the first function call.

Brandon-Schefstad commented 1 year ago

I found a solution but it would interfere with other services the web browser may be using with setInterval(). Unassigning myself for now to open the issue up to anyone else!

My Solution:

function handleTimer(duration) {
    setInterval(function countdownTimer() {
        if(timerObject.active === true){
            timerObject.elapsedTime = timerObject.elapsedTime + 1;
            console.table('elapsedTime', timerObject.elapsedTime);
            // display timer and update task within db
            [minutes, seconds] = calculateTimer(duration-1);
            displayTimer(minutes, seconds);
        }
        if (--duration < 0) {
            ClearAllIntervals()
            displayTimer('00','00');
            updateTimerObject();
        }
        if (timerObject.active === false) {
            ClearAllIntervals()
            changebuttonColor('red')
        }
        return countdownTimer
    }(), 1000); // <- Interval in ms
    changebuttonColor('white')
}

function ClearAllIntervals() {
    for (var i = 1; i < 99999; i++)
            clearInterval(i);
}