Angular.js's $timeout directive makes a package for Window.settimeout, whose return value is a Promise object. When the defined time is up, the Promise object is resolve and the callback function is executed.
If you need to cancel a timeout, call the $timeout.cancel (Promise) method.
Usage:
$timeout(fn, [delay], [invokeapply]);
fn: callback function (required)
Delay:number type. The delay time (not required), if not filled, indicates that the thread will execute after it is empty. For example, when the page is rendered.
Invokeapply: boolean value. If dirty value detection is required (not required), the default is False, and if set to true, the FN callback is wrapped in $scope. $apply () execution
Return value: Returns a Promise object. When the defined time is up, the value of the Promise object will be resolve.resolve the return value of the FN callback function.
Method:
$timeout. Cancel ([promise])
Promise: A Promise object created by $timeout (). (not required). After you call Cancel (), the Promise object is reject.
Return value: If the callback for $timeout () has not been executed, the cancellation succeeds. Returns True
Here's a simple test:
var Timeoutapp = angular.module (' Timeoutapp ', []); Timeoutapp.run (function ($timeout) {
var a = $timeout (function () {Console.log (' Execute $timeout callback ');
Return ' angular '},1000); A.then (function (data) {Console.log (data)},function (data) {Console.log (data)} ); $timeout. Cancel (a); })
Run to see the console print later:
Executes the $timeout callback angular
If I open the comment and execute the. Cancel () method, then the $timeout callback is not executed, and the promise returned by the reject is printed by the console:
Canceled
Angular.js's $timeout