1

I am using angularJS in my app. I have timer with $interval. When I open browser, it works, but sometimes it`s stops.
I tried these cases:

  • Open tab and wait for 15-20 minutes
  • Open tab as not active and wait for 15-20 minutes
  • Lock windows and wait again 15-20 minutes

In these cases all work well, but when I locked windows for 4 hours I found that timer stopped updating after an hour.

Also there are some cases that timer is being stopped when Windows is not locked, so can you help me with this issue?

function setTime() {
        var now = new Date(timeStamp);
        $scope.time = new Date(new Date( now.getTime() + (now.getTimezoneOffset()  60000)).getTime() + ($scope.timezoneOffset  60000));
        if (!_intervalId) {
            _intervalId = $interval(function () {
                var tempTime = new Date().getTime();
                timeStamp += (tempTime - currentTime) > 1100 || (tempTime - currentTime) < 900 ? 1000 : tempTime - _currentTime;
                _currentTime = tempTime;
                var now = new Date(timeStamp);
                $scope.time = new Date(new Date( now.getTime() + (now.getTimezoneOffset()  60000)).getTime() + ($scope.timezoneOffset  60000))
            }, 1000)
        }
    }
1
  • it has nothing to do with js or angular Commented Dec 11, 2015 at 11:19

1 Answer 1

1

Some browsers do a thing where they stop long running tasks to save battery. It's not really documented anywhere very well as far as I know. It's possible to configure it in some browsers, in everyone's favorite (sigh) IE - it's in windows power options:

enter image description here

So this is really happening, you're not losing your sanity. The browser is trying to conserve battery power on sites running long-running tasks.

I recommend you detect activity on the page (a mousemove for example) and restart the interval if it did update a counter you keep for this purpose in a second. Dirty but will likely work.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.