Home  >  Article  >  Web Front-end  >  Detailed explanation of the usage of $interval in AngularJS_AngularJS

Detailed explanation of the usage of $interval in AngularJS_AngularJS

WBOY
WBOYOriginal
2016-05-16 15:16:331636browse

In AngularJS $interval is used to handle intermittent processing of some things.

The most commonly used 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");
}
}) 

The above does one thing every 100 milliseconds, all of which were calling the then function last night. That is, $interval provides a callback function.

Can I control the number of times I do something?

--可以的。
var timer = $interval(function(){},100,10);

Above, the last actual parameter 10 is the limit number of times.

In addition to calling the callback function after everything is finished, what other callback functions are there?

--Yes, it also includes callback functions every time an event is called, and callback functions when an error occurs.

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("每次都更新");
}

Is it possible to cancel the $interval service?

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

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

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn