Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

angularjs $timeout without delay parameters reason

Tags:

angularjs

In a few script I can find for instance

$timeout(function () {     $scope.my = 1;             }); 

instead of simply

$scope.my = 1; 

What's the purpose to call $timeout without delay?

like image 979
Whisher Avatar asked Dec 16 '13 12:12

Whisher


People also ask

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 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 flush?

Verifies that there are no pending tasks that need to be flushed. It throws an error if there are still pending tasks. This method is essentially an alias of $verifyNoPendingTasks (called with no arguments).


1 Answers

This is a hack. :) But usually the intention is to wait until the end of the $digest cycle and then set $scope.my to 1. Timeouts are called after all watches are done.

like image 165
Davin Tryon Avatar answered Sep 23 '22 19:09

Davin Tryon