1
var four = function() {
    $scope.text += '4';
}

var five = function() {
    $scope.text += '5';
}

$scope.text = '1';

$timeout(function () {
    $scope.text += '2'
});

$timeout($scope.text += '3');

$timeout($scope.$eval(four));

$timeout(five);

Result: 13425

According the call sequence the result should be 12345. The lines below are executed immediately:

$timeout($scope.text += '3');
$timeout($scope.$eval(four));

And if you add the time parameter like below, the time is ignored.

$timeout($scope.text += '3', 1000);
$timeout($scope.$eval(four), 1000);

https://jsfiddle.net/uj9yx9c7/1/

3
  • 2
    That's how JavaScript works. That's why $timeout takes a function Commented Nov 25, 2016 at 13:25
  • What's an "inline command"? Commented Nov 25, 2016 at 13:32
  • I would refer to expression. I fixed the title. Commented Nov 25, 2016 at 14:17

1 Answer 1

4

$timeout($scope.text += '3');

in this line $scope.text += '3' is not a function , but an expression. so it will get executed after $scope.text = '1';.

To defer a statement, you need to wrap that statement with a function and pass it $timeout or setTimeout. you can't defer a statement.

Replace $timeout($scope.text += '3'); with $timeout(function(){$scope.text += '3'}). you will get the output 14235.

And $scope.$eval() evaluates a function or an expression synchronously so 4 will get added before 235. changing it to

$timeout(function(){$scope.$eval(four)})

will get the expected output 12345

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

Comments

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.