Isetty Ravitejakumar Isetty Ravitejakumar - 1 month ago 10x
AngularJS Question

difference between setTimeout in javascript and $timeout service in angularjs

Iam new to angular framework.Here is my scenario where, I want to change my $scope.variable after a period of time so i used javascript




This code doesn't worked for me. I used
to make this code work.

Later i came to know that angular itself has a $timeout service which does the same work.

$timeout(function () {
}, 2000);

How can i compare performance of
service with javascript

Why we should use
instead of

Please give me some examples and reasons to use it, which shows the performance.

Thanks :)


Any AngularJS scope variable when handled from outside (including ajax) needs a $apply().

$timeout() takes care of the current scope and runs in the same digest cycle.

One beauty of $timeout() that I recently discovered is, if you do not pass the time parameter, it will wait for the DOM completion.


  console.log("show after directive partial loaded")
}); //note, no time parameter

Run this code in a directive and the timeout callback function will be fired once the partial has been loaded by the directive

Hope this helps.