$ timeout在angular.js中无效

时间:2014-10-03 13:59:56

标签: javascript angularjs

我在我的控制器中使用$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);

}]);
  • 问题是什么。

2 个答案:

答案 0 :(得分:10)

<强>已更新

只需替换

$timeout(function() {
    getNotifications();
}, 1000);

$interval(function() {
   getNotifications();
},1000);

检查Angular's doc

答案 1 :(得分:1)

在angularJS中调用超时时,只需删除括号或方括号()

$timeout(getNotifications, 1000);

getNotifications 函数将在1000毫秒(一秒)后调用