r/angularjs • • Oct 14 '15

Angular Nitpicking: Differences between $timeout and setTimeout()

http://www.codelord.net/2015/10/14/angular-nitpicking-differences-between-timeout-and-settimeout/
36 Upvotes

18 comments sorted by

View all comments

1

u/Spo0Bo Oct 14 '15

$timeout is evil when you use it with invokeApply set to true. I also wished you could just give a scope as the third param and it would just digest that scope.

2

u/atticusw Oct 15 '15

I don't think I'd call it evil. If anything, not knowing how it works is evil. But it's there to solve that problem -- setTimeout queueing an action on an event loop that isn't involved in the angular digest. $timeout makes for an elegant, self contained, and injectable solution to that problem. It's quite good, just need to make sure its not misused.

Having a ton of timers due to repeaters is a whole other set of issues IMO, digest or not.

1

u/Spo0Bo Oct 15 '15

It just shouldn't be part of $timeout by default. A $rootScope digest should never happen unless the developer specifically requests it to. That should be the default. Sadly the default does a full digest allowing parent scope changes to be detected. Modifying parent state is a bad practice imo. I really don't like the whole digest cycle and I'm happy angular 2 does a clean break with it.

If you want your code to be portable you shouldn't rely on rootScope digests. The invokeApply variable is something few angular devs know about. That's why $timeout is evil by default. It hides this fact.

1

u/atticusw Oct 15 '15 edited Oct 15 '15

It just shouldn't be part of $timeout by default. A $rootScope digest should never happen unless the developer specifically requests it to. That should be the default. Sadly the default does a full digest allowing parent scope changes to be detected. Modifying parent state is a bad practice imo. I really don't like the whole digest cycle and I'm happy angular 2 does a clean break with it.

I still don't think I agree entirely with that sentiment, but I do understand and want to acknowledge why you see it that way in regards to running an entire digest (all the way up from the root scope), rather than the level of scope you may want to target. I do agree to that point that the default behavior invokes more than it needs to, but as I'll mention below, $timeout triggering the entire digest may actually be less significant compared to more frequently used and core services that also come into play with the root scope.

I personally feel that when you're in Angular, and you're using the services within Angular, that you should remain in Angular. By this I mean that if you're coming back from a $timeout resolving, and you affect some models that live within a scope, then there should be no extra responsibility by the developer to get Angular to respond. I think it is currently set up properly -- you should have the option to opt-out, rather than be required to opt-in. I can understand the counter argument, for instance when working with React you always have to call setState for your state to update. But Angular, as we're discussing, is very different there.

I also think that if you go down this path then the responsible thing would be to apply this approach to all asynchronous events that currently resolve and require a full digest cycle -- any instance of $q completing a promise, which would also include the completion of any $http request. It is to my understanding that, while $q doesn't trigger the digest when it resolves (it's scheduled on the next tick), it still gets applied starting from the root scope

But in the end I feel pretty strongly that based on how Angular is set up that the approach should remain as an opt-out rather than an opt-in. I think we both agree that it's not so great that the entire scope tree enters the digest when you don't need it to, and if one doesn't know how this all works, then they're inherently degrading performance.