Angular $ timeout or Javascript timeout?

2

I am a beginner in Angular JS. I noticed that we have a service in Angular called $timeout .

I was curious to understand why this service exists, since natively has setTimeout .

What is the difference between $timeout and setTimeout ?

    
asked by anonymous 25.07.2016 / 18:15

2 answers

2

$timeout automatically executes $scope.$apply() after executing callback , thus propagating any and all changes to the template performed by callback .

$timeout also provides, as a callback, a promise .

    
25.07.2016 / 18:35
0

"The idea is basically the same", even in the documentation it is said that $timeout is a wrapper of window.setTimeout . The advantage of using $timeout is that it serves as a Promise , thus avoiding Callback Hell if you need to chain behavior after the expected time. It also has the advantage of being an "inject service" it can easily be replaced by another implementation.

    
25.07.2016 / 18:37