【发布时间】:2016-08-02 18:20:29
【问题描述】:
我需要一个可以按下一次的按钮来执行单个命令。但是也应该可以按住按钮并在按住按钮的同时多次执行命令。我正在使用AngularJs(虽然我不认为它与问题有关)
到目前为止我所拥有的:
<button type="button"
class="btn btn-default"
ng-click="ChangeSetPoint('Up')"
ng-mousedown="startLoopingUp()"
ng-mouseup="stopLoopingUp()"
ng-mouseleave="stopLoopingUp()">
+
</button>
在控制器中:
$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);
}
在我使用这个“方向”参数之前,它有点工作。在我在 setTimeout 中使用 arguments.callee 之前,但是当我查看如何使用该函数传递参数时,我注意到不鼓励使用 arguments.callee (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/arguments/callee)。从那时起,我收到“超出最大调用堆栈大小”错误。
【问题讨论】:
-
@CoderHawk,我看到了这个问题并启发了我最初的解决方案。但是使用指令是另一种方法。
标签: javascript angularjs button onmousedown