user137717
user137717

Reputation: 2165

$interval not repeating task in Angular

I'm writing a test for my web app so people can click a button and see the application run with some randomly generated values. This is an angular application and this test function lives inside a controller. I'm trying to slow down the function execution because my server / browser can't keep up if I just let it run. I am trying to use $interval to run my function once every 500 ms, but I click the button and it only runs once, then stops.

Here is the test function:

$scope.test = function (){
      console.log('in the test function')
      $interval($scope.sendData(), 500, 100)
}

and here is $scope.sendData():

$scope.sendData = function () {
   console.log('send Data')
   var val1 = parseInt(Math.random()*1000)
   var val2 = parseInt(Math.random()*1000)
   var val3 = parseInt(Math.random()*1000)
   var val4 = parseInt(Math.random()*1000)
   var val5 = parseInt(Math.random()*1000)
   var val6 = parseInt(Math.random()*1000)
   var str = 'val1=' + val1 + '&val2=' + val2 + '&val3=' + val3 + '&val4=' + val4 + '&val5=' + val5 + '&val6=' + val6
   $http.get('http://localhost:3002/sendFootData?'+str)
}

I also tried setInterval and setTimeout, but they did not slow down the function execution and my browser locked up because it couldn't keep up with the server responses.

Upvotes: 1

Views: 574

Answers (1)

dfsq
dfsq

Reputation: 193261

You need to pass function reference to the $interval:

$interval($scope.sendData, 500, 100);

In case you need to pass additional parameters you can to use additional anonymous function:

$interval(function() {
    $scope.sendData(param1, param2);
}, 500, 100);

Upvotes: 2

Related Questions