Home > Web Front-end > JS Tutorial > body text

Detailed explanation of the usage of $interval in AngularJS_AngularJS

WBOY
Release: 2016-05-16 15:16:33
Original
1614 people have browsed it

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");
}
}) 
Copy after login

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);

Copy after login

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

Is it possible to cancel the $interval service?

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

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

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!