0

I'm working on a kind of stopwatch/alarm, that's supposed to be running on a website using a modified wordpress plugin. It's function will later be, to start the stopwatch, and once it reaches 15 minutes, there will be an alarm sound. If the watch then doesn't get reset within the next 3 minutes, there will be another action (notification or something, not important right now, that part I already figured out), and if it's reset, it starts the whole thing again (will run for multiple hours).

I've set up the infrastructure via wordpress, and now my only problem is the stopwatch itself. My problem right now is, that it "kind of" stops running, whenever someone moves to another tab or program. With "kind of", I mean: If the alarm sound was played ONCE, and I then reset it, it keeps counting even when switching tab after that. But on the first page refresh, it doesn't; except sometimes it does, but very slowly (1 "second" takes 3 real seconds). This is what my code looks like right now:

var silence = new Audio('exampleSilence.mp3');      //Audio-file with no content (so just silence)
var audio = new Audio('exampleAlarm.mp3');          //Audio-file with random song/alarm sound

var time=0;
var running=0;
function strtpause () {                            //Function responsible for start/pause button
    if(running==0){
        running=1;
        increment();
        document.getElementById("strtpause").innerHTML="Pause"
    }
    else{
        running=0;
        document.getElementById("strtpause").innerHTML="Resume"
        return;
    }
}
function reset(){   //Function responsible for resetting the stopwatch timer. At first also stopped 
                    //the counting, but I disabled that part as I want it to keep going
    //running=0;
    time=0;                                                 //Sets time back to 0, so timer can start from 0 again
    //document.getElementById("strtpause").innerHTML="Start"
    document.getElementById("output").innerHTML="00:00"
    audio.pause();                                          //Will also stop the alarm
    audio.currentTime = 0;                                  //And set the alarm time back to 0
}
function increment(){                                       //The actual stopwatch timer function
    if(running==1){
        setTimeout(function(){
            time++;
            var mins=Math.floor(time/60);
            var secs=Math.floor((time)-(mins*60));
            if(mins<10){
                mins="0"+mins;
            }
            if(secs<10){
                secs="0"+secs;
            }
            document.getElementById("output").innerHTML=mins+":"+secs;
            
            if(1<time<9){                           //I attempted to play a silent audio file in the background
                silence.play();                      //to keep the process running, but didn't have an effect
            }
            if(1<time<9){                           //Also tried playing the audio file but very very silent.
                audio.volume = 0.001;               //It did work, but made timer go slower than regular seconds
                audio.play();
            }
            if (time>10){                          //Once the timer reaches a certain time, the alarm is played
                audio.volume = 0.5;                //Adjusts sound volume
                audio.play();
            }
            increment();
        },1000);            
    }
}

My solution ideas until now were:

-Change the setTimeout increments -> No effect, except with smaller increments it seemed to go slower

-Let it play a "silent" audio file with no actual sound content, so it "has something to do" (no idea if that makes sense, but audio files keep playing in background)

-Let it play an audio file with content while waiting, but very quietly (volume = 0.001). Did work, but made the stopwatch go way too slow, with 1 "second" = 3 actual seconds.

Ideas on how to keep the code running on any OS/Browser are appreciated! I'd prefer not to write/setup a different file or language, as my webdevelopment skills are very very basic, and I don't have rights to edit everything on the website.

Hillel
  • 811
  • 2
  • 7
  • 19
Tobse
  • 1
  • 3

1 Answers1

0

Time delay for setTimeout() / setInterval() methods executing on inactive browser tabs are set to one second regardless of their value defined in code.

More about that here: https://usefulangle.com/post/280/settimeout-setinterval-on-inactive-tab

Hillel
  • 811
  • 2
  • 7
  • 19
  • Interesting...for my purposes, this should actually be a good thing, as I want the timer to take 1 seconds anyways (aka 1000ms). But that also means, that I can't intentionally set the timeout to, say, 900ms, to compensate for the time needed to process sth like the volume = 0.001 – Tobse Jun 12 '22 at 10:35
  • I think what you're looking for can be found here: https://stackoverflow.com/questions/36211993/audiocontext-timing-issues-when-window-gets-minimized – Hillel Jun 12 '22 at 11:03