Timers(setInterval) in ionic apps go to sleep after some time in the background

10,819

Solution 1

After hours of searching for an answer, I finally came up with my own hack. I hope this solution might help others who come across a similar issue.

When the app goes to the background, at some random time, the timer stops ticking and goes to sleep till the app is brought back the foreground. When the app comes up to the foreground, the timer starts ticking again from the point where it went to sleep.

  • Solution/Hack:

    1. Record the timestamp in a separate variable(in seconds) and have it updated in each interval of the timer.

      var timeStamp = Math.floor(Date.now() / 1000);

    2. Check each interval of the timer if the difference between your previous interval's timeStamp and the latest(new) timeStamp is greater than one second. If the condition is met, add the difference between those two timestamps to your ticking time.

  • How it works:

    App in Foreground

    1. Just Before timer start ticking
      - Time stamp recorded (Assume 1 second)
    2. Timers start ticking
      - check condition if(currentTimeStamp - previousTimeStamp > 1) { Add the the above difference to the time } Before the interval ends, update the TimeStamp variable with the currentTimeStamp.
      In the first interval, the currentTimeStamp should be either 1 second or 2 second depending on weather you are offloading the timer into a setTimeout. Thus the difference will definitely be 0 or 1. Since the condition doesn't match we update the timestamp with 1 or 2 seconds and move on to the next interval. As long as the timer doesn't go to sleep our condition will fail.

    App in Background

    Strangely after 10 minutes, the timer goes to sleep(our timer is literally losing track of time from now because the next interval is not firing).

    App return from Background to foreground

    The timer starts ticking from where it stopped(i.e. the next interval). Now the difference in our condition should be more than one second and thus adding that difference(basically the lost time) to our current ticking time.

  • Code:

    var transactionTime = 0; //Initial time of timer
    var timeStamp = Math.floor(Date.now() / 1000);
    var deltaDelay = 1;
    
    setInterval(function () {
        if (transactionTime != 0 && (Math.floor(Date.now() / 1000) - timeStamp) > deltaDelay) {
                transactionTime += (Math.floor(Date.now() / 1000) - timeStamp);
            }
            timeStamp = Math.floor(Date.now() / 1000);
    
            //Update your element with the new time.
            window.document.getElementById("transaction_timer").innerHTML = util.formatIntoHHMMSS(transactionTime++);
    
        }, 1000);
    

    Note: This solution work standalone(vanilla Js with native DOM api) and also works great in angular directives.
    You can increase the deltaTime of the above code to 2 to be a little more accurate if by any chance your single thread is busy somewhere else with some other task.
    P.s I'm actually running the ionic app inside my own instance of a webview and not cordova so I can't use any fancy cordova plugin.

    Solution 2

    Ionic/Cordova apps goes to sleep when in background mode. But you could look at this: https://github.com/katzer/cordova-plugin-background-mode

  • Share:
    10,819
    Lakmal Caldera
    Author by

    Lakmal Caldera

    Hi guys, I'm currently working as an native ios developer based in Srilanka with more than one year of experience in various other mobile technologies such as android, phonegap, jquery mobile, etc. I consider myself to be passionate programmer, but in my free time I like to hit the gym, read up material on how to be real life hacker (enough to satisfy my imagination atleast lol) "Life is short, take chances and live your dreams.. its never too late."

    Updated on June 13, 2022

    Comments

    • Lakmal Caldera
      Lakmal Caldera about 2 years

      My ionic app has a timer(a simple setInterval that ticks every second) which works perfectly fine when the app is in the foreground. However when the app goes to the background and comes back to the foreground after 10 minutes, the time displayed in the app is wrong (the time is much less that it should be). I have tried adding the timer into a directive and also using the native DOM manipulation api(document.getElementById, etc) methods, but they both didn't work. I think the ionic framework is doing something to the view and bindings when the app goes to the background. Has anyone experience such a issue and if so how did you guys manage to fix it?

    • minigeek
      minigeek almost 4 years
      simply genius! you are an angel to me
    • minigeek
      minigeek almost 4 years
      app store rejects it if not genuine reason like music player .. not recommeded