Don
Don

Reputation: 6872

Using timeout instead of $timeout in Angular 1.4

I'm implementing a hack in order to scroll the contents of an element using an Angular 1.4 directive. However the code to change the scroll position of the element needs to be done after x seconds of clicking the same element.

This directive does not absolutely nothing but change the scroll position of an HTML element.

Should I be using the native browser setTimeout or $timeout function. I feel that using $timeout is unnecessary as this will trigger the digest cycle (as mentioned this is unnecessary).

So my question really boils down to: Is this a good idea and are there similar or other scenarios where the use of the native setTimeout over $timeout function is warranted.

Upvotes: 0

Views: 298

Answers (1)

Claies
Claies

Reputation: 22323

AngularJs already accounts for this. $timeout and $interval both accept an optional parameter which will allow you to skip dirty checking: invokeApply which is set to true by default.

$timeout(someFunction, 1000, false) will skip the $digest. You should always use $timeout over setTimeout. $timeout is a wrapper around setTimeout that is able to handle exceptions through the $exceptionHandler.

https://docs.angularjs.org/api/ng/service/$timeout/

Upvotes: 2

Related Questions