Sami
Sami

Reputation: 117

how to interval call with parameter angularjs

how to $interval call function with parameter

$interval( getrecordeverytime(2), 100000);
 function getrecordeverytime(contactId)
        {
          console.log(contactId + 'timer running');
         }

Upvotes: 1

Views: 1823

Answers (3)

Nikolaj Dam Larsen
Nikolaj Dam Larsen

Reputation: 5674

Alternatively you can create a function that returns the interval callback function, and have the parameter bound to the callback through closure. Like this:

function createRecordCallback(contactId){
    return function(){
        console.log(contactId + 'timer running'); // the value of contactId, will be bound to this function.
    };
}

$interval(createRecordCallback(1234), 100000);

This is merely meant as an alternative. I do recommend Slava's answer in most cases.

Upvotes: 0

Slava Utesinov
Slava Utesinov

Reputation: 13488

You can pass parameters starting from fifth argument of $interval:

angular.module('app', []).controller('ctrl', function($scope, $interval){
  function getrecordeverytime(contactId, second) {
      console.log(`${contactId}, ${second} timer running`);
  };
  $interval(getrecordeverytime, 1000, 0, true, 2, 5);
})
<script src="//code.angularjs.org/snapshot/angular.min.js"></script>

<div ng-app='app' ng-controller='ctrl'>
</div>

Upvotes: 2

Ramesh Rajendran
Ramesh Rajendran

Reputation: 38683

Try this .

        function getrecordeverytime(contactId){
          console.log(contactId + 'timer running');
        }    
        $interval(function(){getrecordeverytime(2)},100000);

Upvotes: 0

Related Questions