3

JS:

var vm = this;
vm.loadNames = function () {
    var promise = service.getNames();
    promise.then(function (data) {
        $scope.names = data.names.data;
        $timeout(vm.loadNames, 5000);
    });
};
var timer = $timeout(vm.loadNames, 5000);
$scope.canceltime = function(){
        $timeout.cancel(timer);
    };

HTML:

<button ng-click="canceltime()"></button>

I want to stop $timeout after click button. My code don't work. Thanks for answers in advance!

1 Answer 1

4

you need to declare timer variable globally. try this code. does this help

 var vm = this;
    var timer;
    vm.loadNames = function () {
        var promise = service.getNames();
        promise.then(function (data) {
            $scope.names = data.names.data;
           timer = $timeout(vm.loadNames, 5000);
        });
    };
    $scope.canceltime = function(){
            $timeout.cancel(timer);
        };

      $scope.mouseout = function(){
        timer = $timeout(function () {
          $scope.show = false;
        }, 2000);
      };

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

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.