33

In a few script I can find for instance

$timeout(function () {
    $scope.my = 1;            
});

instead of simply

$scope.my = 1;

What's the purpose to call $timeout without delay?

1 Answer 1

54

This is a hack. :) But usually the intention is to wait until the end of the $digest cycle and then set $scope.my to 1. Timeouts are called after all watches are done.

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

2 Comments

Davin, could you explain WHY it is a hack? Maybe provide an example of when it might be used and what the ACTUAL IDEAL solution would be? I notice it is quite prevalent in a project at work, and I understand what it fixes, but I can't quite figure out what a better solution would be.
Exemple : I have a click event which trigger an apply on the current scope and I have a watcher which triggers the same click event. I had to make a condition to not reload the apply call into the event and a timeout into the watcher to wait the end of the digest cycle (due to apply() called in the event). I'll post the code when soon.

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.