1

how to $interval call function with parameter

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

3 Answers 3

2

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>

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

Comments

0

Try this .

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

Comments

0

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.

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.