The use of $interval in AngularJS is explained in detail

  • 2020-12-13 18:50:03
  • OfStack

In AngularJS $interval is used to deal with intermittent processing of 1 thing.

The most common ones are:


var app = angular.module("app",[]);
app.controller("AppCtrl", function($q. $interval){
var timer = $interval(function(){
},100);
timer.then(success);
function success(){
console.log("done");
}
}) 

Above, do one thing every 100 milliseconds, all calling the then function last night. That is, $interval provides a callback function.

Can you control the number of times you do it?


-- You can. 
var timer = $interval(function(){},100,10);

Above, the last argument 10 is the limiting number.

What other callback functions are there besides calling after everything is done?

Yes, it also includes callbacks for each event called and for errors.


var timer = $interval(function(){},100, 10);
timer.then(success, error, notify);
function success(){
console.log("done");
}
function error(){
console.log("error");
}
function notify(){
console.log(" Update every time ");
}

Can I cancel the $interval service?


-- through $interval.cancle(timer);
var timer = $interval(function(){},100, 10);
this.cancel = function(){
$interval.cancel(timer);
}

The above is a detailed explanation of the use of $interval in AngularJS. I hope it will be helpful to you.


Related articles: