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!
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);
};
});