这是我的代码: JS:
var timer;
$scope.getapi_url = function(n){
var url = n;
$http({
method: 'GET',
url: url,
})
.then(function successCallback(data) {
$scope.data = data.data;
console.log($scope,data);
timer = $timeout($scope.getapi_url(), 5000);
}, function errorCallback(response) {
$scope.errorBackend = true;
console.log(response);
console.log('error');
});
};
HTML:
<button class="btn btn-clear btn-sm" ng-click="getapi_url('myurl') ">Click!</button>
在第一个$timeout
之后,我抛出了错误,类似n is undefinded
我该怎么办?
提前感谢所有答案!
答案 0 :(得分:0)
尝试一下,但它永远不会停止,您必须使用$timeout.cancel(timer);
手动停止。
var timer;
$scope.getapi_url = function (n) {
var url = n;
$http({
method: 'GET',
url: url,
})
.then(function successCallback(data) {
$scope.data = data.data;
console.log($scope, data);
timer = $timeout($scope.getapi_url(n), 5000);
// passing n if you don't want check n value on starting of function.
}, function errorCallback(response) {
$scope.errorBackend = true;
console.log(response);
console.log('error');
});
};