How to stop $timeout in AngularJS?

Viewed 1228

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 Answers
Related