Angular $timeout or Javascript timeout?

Asked

Viewed 387 times

2

I’m a beginner in Angular JS. I realized that we have a service in Angular called $timeout.

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

What is the difference between the $timeout and the setTimeout?

2 answers

2


$timeout automatically runs $scope.$apply() after the execution of callback, thus propagating any change in the model made by callback.

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

  • The main question is: If I use setTimeout, there is a risk that a data will not be updated at the angle. I thought that was the reason, but I see that it is much more.

0

"The idea is basically the same", including 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 avoids the Callback Hell if you need to chain behavior after the expected time. It also has the advantage of being an "injected service" it can be replaced easily by another implementation.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.