Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 setTimeout method.

$scope.variable = 'Previous';  setTimeout(function(){   $scope.variable='NEXT'; },3000); 

This code doesn't work for me. I used $apply() to make this code work.

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

$scope.variable = 'Previous';  $timeout(function () {   $scope.variable = 'NEXT'; }, 2000); 

How can i compare performance of $timeout service with javascript setTimeout??

Why we should use $timeout instead of setTimeout??

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

Thanks :)

like image 404
Ravi Teja Kumar Isetty Avatar asked Sep 03 '16 20:09

Ravi Teja Kumar Isetty


People also ask

What is the difference between $timeout and setTimeout?

setTimeout in order to display an alert message after a timeout of at least 2000 milliseconds. Angular $timeout is a wrapper written for window. setTimeout in form of a try catch block which throws exceptions via $exceptionHandler service. $timeout accepts the function to be delayed, delay time, a boolean to invoke $.

What is $timeout in AngularJS?

This '$timeout' service of AngularJS is functionally similar to the 'window. setTimeout' object of vanilla JavaScript. This service allows the developer to set some time delay before the execution of the function.

What is difference between setTimeout and setInterval in JavaScript?

setTimeout allows us to run a function once after the interval of time. setInterval allows us to run a function repeatedly, starting after the interval of time, then repeating continuously at that interval.

What is the function of the $timeout service?

The $timeout service can be used to call another JavaScript function after a given time delay. The $timeout service only schedules a single call to the function. For repeated calling of a function, see $interval later in this text.


2 Answers

There are some cases where one needs to perform some sort of timeout operation and we frequently achieve this using JavaScript's setTimeout() function.

However, if we use setTimeout() in an AngularJS application we also need to use $scope.$apply() to ensure that any changes to the scope will be reflected elsewhere (i.e. data-bound in a view).

AngularJS provides a handy wrapper for this: $timeout() - it does the $apply() for which we don't have to $apply the changes.

Regarding the performance.

If you're using $timeout to create what is essentially an interval, then don't use it. If your application is large then $apply will also trigger a $digest cycle which you may not really want it to happen, it will surely decrease the performance.

like image 61
Nikhilesh Shivarathri Avatar answered Sep 22 '22 09:09

Nikhilesh Shivarathri


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 after all the change detection is done.

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

So,

$timeout(function(){   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.

like image 22
Tarun Avatar answered Sep 21 '22 09:09

Tarun