Home  >  Article  >  Web Front-end  >  AngularJS中$interval的用法详解_AngularJS

AngularJS中$interval的用法详解_AngularJS

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

在AngularJS中$interval用来处理间歇性处理一些事情。

最常用的是:

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

以上,每隔100毫秒就做一件事,所有都昨晚在调用then函数。也就是,$interval提供回调函数。

是否可以控制做事的次数呢?

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

以上,最后一个实参10就是限制次数。

回调函数除了在所有事情结束后调用,还有什么其它回调函数?

--有的,还包括每次调用事件时的回调函数,和出现错误时的回调函数。

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

是否可以取消$interval服务呢?

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

以上所述是针对AngularJS中$interval的用法做的详解,希望对大家有所帮助。

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