4

I am using angularjs $interval function to increase/decrease a value continuously when I click on up/down arrows. But I am getting an error "TypeError: $interval is not a function" when I try to increase/decrease. How to solve this problem, thanks in advance. This is the code I have written so far:

$scope.onMouseDown = function (type) {
  promise = $interval(function () {
    if (type == 'Inc') {
       $scope.increaseVal();
    }
    else if (type == 'Dec') {
       $scope.decreaseVal();
    }
  }, 150);
};
$scope.stopInterval = function () {
   $interval.cancel(promise);
};
<div><a href="javascript:;" class="arrow" data-spin="up"><i class="fa fa-caret-up" ng-mousedown="onMouseDown('Inc')" ng-mouseup="stopInterval()" ng-mouseleave="stopInterval()" ng-click="increaseVal()"></i></a>
<a href="javascript:;" class="arrow" data-spin="down"><i class="fa fa-caret-down" ng-mousedown="onMouseDown('Dec')"
ng-mouseup="stopInterval()" ng-mouseleave="stopInterval()" ng-click="decreaseVal()"></i></a>
</div>

4
  • have you included $interval in the controller dependencies? Commented Jan 18, 2017 at 15:15
  • 1
    Even reading documentation would answer your question. Commented Jan 18, 2017 at 15:17
  • read docs.angularjs.org/api/ng/service/$interval . there are examples as well about the usage Commented Jan 18, 2017 at 15:18
  • What do you increaseVal and decreaseVal functions look like? @dev8080 Wouldn't it say cannot read property cancel of undefined if that was the case? Commented Jan 18, 2017 at 16:31

1 Answer 1

2

Inject the $interval service into your controller:

angular.module('yourmodulename').controller('yourcontrollername', [
    '$scope', '$interval',
    function ($scope, $interval) {
       $scope.onMouseDown = function (type) {
           promise = $interval(function () {
               if (type == 'Inc') {
                   $scope.increaseVal();
               }
               else if (type == 'Dec') {
                   $scope.decreaseVal();
               }
           }, 150);
       };
       $scope.stopInterval = function () {
           $interval.cancel(promise);
       };
}]);
Sign up to request clarification or add additional context in comments.

3 Comments

Wouldn't it say cannot read property cancel of undefined if that was the case?
What line is the error being thrown? I'm guessing it's where you are trying to set the promise variable, in which case you are using $interval as a function. You aren't even getting to the code where you are trying to call cancel on the $interval service.
Thank you guys, actually I am calling this function from multiple ways. I missed the injection of $interval from one calling function. Now it is working fine. Apologies for troubling you all.

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.