0

i am using $timeout in my controller but its not working!

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 .
2
  • What does not working mean? Is it failing with an error, just not being called, etc.? Commented Oct 3, 2014 at 14:02
  • the console is empty no error the controller successfully pass data to view but its not repeat the function (ajax call) Commented Oct 3, 2014 at 14:05

2 Answers 2

10

UPDATED

Just replace

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

with

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

Check Angular's doc

Sign up to request clarification or add additional context in comments.

6 Comments

yes now is working but i have this error in the console Error: [$rootScope:inprog] errors.angularjs.org/1.3.0-rc.4/$rootScope/inprog?p0=%24digest
Note that there is an $interval service. I'm guessing the answer explaining why no $interval existed in August of 2013 is just outdated now :)
can i have to inject the $apply into the controlller?
@user3636439 if you use $interval - no
@user3636439 instead inject $interval (you shouldn't have to inject $apply ... it's a function on the $scope). The main reason to use $interval instead of setInterval() is that it won't cause you headaches in unit tests... plus you don't need to use $apply() b/c Angular will call it for you as needed when using $interval :)
|
1

while calling timeout in angularJS just remove Parentheses or brackets ()

$timeout(getNotifications, 1000);

getNotifications function will call after 1000 milliseconds(One second)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.