I have this piece of code
inputElem.on('keydown', function(event) {
if (event.which === 38) { // up
$scope.inc();
promise = $interval($scope.inc(), 300);
} else if (event.which === 40) { // down
$scope.dec();
promise = $interval($scope.dec(), 300);
}
});
inputElem.on('keyup', function(event) {
if (event.which === 38 || event.which === 40) {
$interval.cancel(promise);
}
});
I am basically using it to increase/decrease the values in an edit box by 1 using the up and down arrows. So when a user keeps the up/down arrow pressed the value keeps changing. My issue is that it seems to increase/decrease the value twice even if I press one of the arrows really quick. I put a console.log() in the inc() and dec() functions and they are indeed called twice even If I change the time to 3 seconds or somethign like that. As far as I know, interval doesn't all the recurring function once it is initialise but rather it waits the corresponding interval the first time. Am I wrong? What could be the problem?