I need a button that can be pressed once to execute a single command. But it should also possible to hold the button and execute the command multiple times while holding the button. I'm using AngularJs (although I don't think it is related to the problem)
What I had so far:
<button type="button"
class="btn btn-default"
ng-click="ChangeSetPoint('Up')"
ng-mousedown="startLoopingUp()"
ng-mouseup="stopLoopingUp()"
ng-mouseleave="stopLoopingUp()">
+
</button>
and in the controller:
$scope.ChangeSetPoint = function(direction){
//Stuff to actually change the setpoint
}
var looping = false;
var promis;
$scope.startLoopingUp = function(){
looping = true;
promis = setTimeout(loop('Up'),1000);
}
var loop = function(direction){
$scope.ChangeSetPoint(direction);
if(looping){
promis = setTimeout(loop(direction),300)
}
}
$scope.stopLoopingUp = function(){
looping = false;
clearTimeout(promis);
}
It kind-of work before I was using this 'direction' parameter. Before I used arguments.callee in setTimeout, but when I looked how to pass a argument with that function, I notices that the use of arguments.callee
was discouraged (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/arguments/callee). Since then I'm getting 'Maximum call stack size exceeded' errors.