首页 > 解决方案 > 将变量作为参数传递给 angularjs 中的 setInterval 函数

问题描述

我需要将一个变量传递给setIntervalangularjs 并在间隔后返回给另一个函数

例子:

$scope.start = function(host_id) {
  // store the interval promise
    $scope[`promise_${host_id}`] = $interval(function(host_id){
        $scope.update_status(host_id)
    }, 3000);
};

// stops the interval
$scope.stop = function(host_id) {
    $interval.cancel($scope[`promise_${host_id}`]);
}; 

$scope.start(5);

标签: javascriptangularjs

解决方案


您可以将参数传递给$interval作为参考示例

$interval(updateTime, 1000, 0,true,'my arguments')// can be array as well

推荐阅读