javascript $timeout 在 angular.js 中不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26180401/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 05:37:08 来源:igfitidea点击:
$timeout not working in angular.js
提问by user3636439
i am using $timeout
in my controller but its not working!
我$timeout
在我的控制器中使用,但它不工作!
app.controller("Friendsrequests",['$http','$log','$location','$timeout','$scope', function($http,$log,$location,$timeout,$scope){
//getting base url of application
this.baseUrl = function() {
var base_url = window.location.origin;
var pathArray = window.location.pathname;
return base_url;
// return base_url+pathArray;
};
// assigning to a variablebase_url('login/f_request');
var ThroughUrl = this.baseUrl()+'/keon/login/f_request';
//declare a variable
var ata = this;
ata.notifications = [ ] ;
ata.counts=' ';
//sending ajax request
function getNotifications()
{
$http({method: 'POST', url: ThroughUrl,})
.success(function(data) {
// this callback will be called asynchronously
// when the response is available
//assign data to the variable
ata.notifications=data;
ata.counts =data.length;
}).
error(function(data, status, headers, config) {
// called asynchronously if an error occurs
// or server returns response with an error status.
});
}
$timeout(function() {
getNotifications();
}, 1000);
}]);
- what is the issue .
- 这是什么问题。
回答by Jevgeni
UPDATED
更新
Just replace
只需更换
$timeout(function() {
getNotifications();
}, 1000);
with
和
$interval(function() {
getNotifications();
},1000);
Check Angular's doc
回答by Badri Chorapalli
while calling timeout in angularJS just remove Parentheses or brackets ()
在 angularJS 中调用 timeout 时只需删除圆括号或方括号 ()
$timeout(getNotifications, 1000);
getNotificationsfunction will call after 1000 milliseconds(One second)
getNotifications函数将在 1000 毫秒(一秒)后调用