如何使用参数 angularjs 间隔调用
how to interval call with parameter angularjs
如何使用参数
调用$interval函数
$interval( getrecordeverytime(2), 100000);
function getrecordeverytime(contactId)
{
console.log(contactId + 'timer running');
}
试试这个。
function getrecordeverytime(contactId){
console.log(contactId + 'timer running');
}
$interval(function(){getrecordeverytime(2)},100000);
可以从$interval
的第五个参数开始传递参数:
angular.module('app', []).controller('ctrl', function($scope, $interval){
function getrecordeverytime(contactId, second) {
console.log(`${contactId}, ${second} timer running`);
};
$interval(getrecordeverytime, 1000, 0, true, 2, 5);
})
<script src="//code.angularjs.org/snapshot/angular.min.js"></script>
<div ng-app='app' ng-controller='ctrl'>
</div>
或者,您可以创建一个 returns 间隔回调函数的函数,并通过闭包将参数绑定到回调。像这样:
function createRecordCallback(contactId){
return function(){
console.log(contactId + 'timer running'); // the value of contactId, will be bound to this function.
};
}
$interval(createRecordCallback(1234), 100000);
这只是一种替代方法。在大多数情况下,我确实推荐 Slava 的答案。
如何使用参数
调用$interval函数$interval( getrecordeverytime(2), 100000);
function getrecordeverytime(contactId)
{
console.log(contactId + 'timer running');
}
试试这个。
function getrecordeverytime(contactId){
console.log(contactId + 'timer running');
}
$interval(function(){getrecordeverytime(2)},100000);
可以从$interval
的第五个参数开始传递参数:
angular.module('app', []).controller('ctrl', function($scope, $interval){
function getrecordeverytime(contactId, second) {
console.log(`${contactId}, ${second} timer running`);
};
$interval(getrecordeverytime, 1000, 0, true, 2, 5);
})
<script src="//code.angularjs.org/snapshot/angular.min.js"></script>
<div ng-app='app' ng-controller='ctrl'>
</div>
或者,您可以创建一个 returns 间隔回调函数的函数,并通过闭包将参数绑定到回调。像这样:
function createRecordCallback(contactId){
return function(){
console.log(contactId + 'timer running'); // the value of contactId, will be bound to this function.
};
}
$interval(createRecordCallback(1234), 100000);
这只是一种替代方法。在大多数情况下,我确实推荐 Slava 的答案。