4

I'm using angularjs and particularly the $timeout service (a wrapper on setTimeout). It works as follows:

 angular.module('MyApp').controller('MyController', ['$scope', '$timeout',
     function($scope, $timeout) {

        $scope.millisecondsLater = 3000000000;
        $timeout(function(){
           console.log('it\'s been ' + $scope.millisecondsLater + ' later');
        }, $scope.millisecondsLater);

    }
 ]);

when this controller is instantiated the function in the timeout gets called immediately. But if I set:

  $scope.millisecondsLater = 2000000000; 

it seemingly doesn't get called, as expected because this is (2000000 secs from now). And of coarse if I set $scope.millisecondsLater = 2000 the callback gets called 2 secs later.

It seems that $timeout has a maximum value somewhere between 3000000000 and 2000000000 and instead of never calling the callback it gets called immediately (for chrome at least). Has anyone run into this before? and how did you cleanly resolve it without a bunch of hard coded if < 2000000000 checks whenever timeouts are used?

Thanks in advance and any insight would be greatly appreciated!

1 Answer 1

5

I do not think this problem is particular to the $timeout service of Angular, but the setTimeout function that exists in normal JavaScript (since $timeout effectively wraps setTimeout anyway).

The maximum value setTimeout can take is a 32-bit integer (i.e. 2147483647). Anything beyond will obviously result in unexpected behaviour. Perhaps break your delays into smaller chunks?

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

1 Comment

I just went ahead and added if($scope.millisecondsLater < 2147483647){ { // perform timeout } Unfortunately, this ignores large timeouts. But since 2147483 seconds is this is like 25 day I'm okay with it... thanks for the response! and the solution of breaking the response into chunks and creating recursive timeouts would be a great wrapper that I will implement at some point.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.