2

I am trying to write an application in Angular which updates data like every 10 seconds. My problem is that the function is only being called once every time i refresh my page.

The console.log is just to ensure that the function is just being called once.

This is my code for initiating the interval:

var app = angular.module('myApp',['ngRoute', 'ui.bootstrap', 'ngNotify', 'ngTagsInput', 'djds4rce.angular-socialshare']);

app.run(function($FB, $http, $rootScope, $interval){
  $FB.init('527807327395199');
  $interval(function() {callAtInterval($http, $rootScope)}, 1000, true);
  $rootScope.count = 0;
});


function callAtInterval($http, $rootScope) {
  $http.get("/points-for-school")
  .then(function(response){
    $rootScope.schoolCompetitionPoints = response.data;
    console.log($rootScope.count++);
    console.log(response);
  }, function(response){
    console.error(response);
  });
}

Is the problem that i do this in the app.run method?

Do i have to put the code into a controller for it to work?

If there is a way of making this work without creating a controller i would prefer it.

3 Answers 3

4

$interval() takes a function as argument, in order to call the function every 10 seconds.

But you're not passing a function as argument. You're calling callAtInterval($http, $rootScope), and passing the value returned by this call (undefined) to $interval(). So you're effectively asking $interval to call undefined every 10 seconds.

What you actually want is

$interval(function() {
    callAtInterval($http, $rootScope);
}, 1000, true);
Sign up to request clarification or add additional context in comments.

2 Comments

Yes that is true. I changed the code accordingly to what you proposed but the it is still only being called once.
You need to add the count parameter as well like i wrote in my answer. Thanks for the help though, it helped me get by the first problem with was how i passed in the function.
1

I know it's late and possibly a few are interested. Probably JB Nizet was right in old version of angularjs. Now is necessary to pass as a paremeter number of times to repeat.

$interval(function() { callAtInterval($http, $rootScope); }, 1000, 0, true);

Parameter 0 will repeat indefinitely.

Here is doc.

Comments

0

I managed to solve the problem. The interval service can have four parameters:

$service(function, delay, count, invokeApply, pass)

The last three parameters is optional. The problem with my code was that the "true" parameter i sent in was read as count with the value 1 and therefore only executed the function once. There was two ways of solving this problem and: 1. remove the "true" parameter since it defaults it to true then. 2. specify that count is undefined

Here is the code i used to make it work:

app.run(function($FB, $http, $rootScope, $interval){
  $FB.init('527807327395199');
  $interval(function() { callAtInterval($http, $rootScope, undefined, true);}, 1000);
  $rootScope.count = 0;
});


function callAtInterval($http, $rootScope) {
  $http.get("/points-for-school")
  .then(function(response){
    $rootScope.schoolCompetitionPoints = response.data;
    console.log($rootScope.count++);
    console.log(response);
  }, function(response){
    console.error(response);
  });
}

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.