Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What advantage is there in using the $timeout in AngularJS instead of window.setTimeout?

I had a suggestion to implement a timeout like this:

  $timeout(function() {      // Loadind done here - Show message for 3 more seconds.     $timeout(function() {       $scope.showMessage = false;     }, 3000);    }, 2000); }; 

Can someone tell me what is the reason / advantage in using this rather than using setTimeout?

like image 406
Samantha J T Star Avatar asked Oct 26 '13 17:10

Samantha J T Star


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 does $Timeout do 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 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.

What we can use instead of setTimeout in JavaScript?

The setInterval method has the same syntax as setTimeout : let timerId = setInterval(func|code, [delay], [arg1], [arg2], ...) All arguments have the same meaning. But unlike setTimeout it runs the function not only once, but regularly after the given interval of time.


1 Answers

In basic words $timeout refers to angularjs when setTimeout - to JavaScript.

If you still think to use setTimeout therefore you need invoke $scope.$apply() after

As a side note

I suggest you to read How do I “think in AngularJS” if I have a jQuery background? post

and AngularJS: use $timeout, not setTimeout

Example 1: $timeout

   $scope.timeInMs = 0;        var countUp = function() {         $scope.timeInMs+= 500;         $timeout(countUp, 500);     }         $timeout(countUp, 500);  

Example 2: setTimeout (same logic)

 $scope.timeInMs_old = 0;        var countUp_old = function() {         $scope.timeInMs_old+= 500;                 setTimeout(function () {         $scope.$apply(countUp_old);     }, 500);     }              setTimeout(function () {         $scope.$apply(countUp_old);     }, 500); 

Demo Fiddle


$timeout also returns a promise

JS

function promiseCtrl($scope, $timeout) {   $scope.result = $timeout(function({   return "Ready!";   }, 1000);  } 

HTML

<div ng-controller="promiseCtrl">   {{result || "Preparing…"}} </div>  

$timeout also trigger digest cycle

Consider we have some 3d party code (not AngularJS) like Cloudinary plugin that uploads some file and returns us 'progress' percentage rate callback.

     // .....      .on("cloudinaryprogress",            function (e, data) {                var name = data.files[0].name;                var file_ = $scope.file || {};                file_.progress = Math.round((data.loaded * 100.0) / data.total);                                                                                  $timeout(function(){                      $scope.file = file_;                 }, 0);                      }) 

We want to update our UI aka $scope.file = file_;

So empty $timeout does the job for us, it will trigger digest cycle and $scope.file updated by 3d party will be re-rendered in GUI

like image 125
Maxim Shoustin Avatar answered Sep 19 '22 14:09

Maxim Shoustin