js angular button hold loop failed

时间:2016-04-12 08:52:16

标签: javascript angularjs button onmousedown

我需要一个按钮,可以按一次执行单个命令。但是也可以按住按钮并在按住按钮的同时多次执行命令。我正在使用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);
        }

在我使用这个'direction'参数之前,这是一种很好的工作。在setTimeout中使用arguments.callee之前,当我看到如何使用该函数传递参数时,我注意到不建议使用arguments.calleehttps://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/arguments/callee)。从那以后我得到'超出最大调用堆栈大小'的错误。

1 个答案:

答案 0 :(得分:0)

这是造成伤害的参数:

更改时

setTimeout(loop, 1000)setTimeout(loop('Up'), 1000)

我没有将函数作为参数给出,而是执行函数并将返回值作为参数。

我应该做的:

promis = setTimeout(function(){ loop('Up') },1000);