Javascript 计数事件,然后使用节流/去盎司执行事件

Javascript 计数事件,然后使用节流/去盎司执行事件,javascript,angularjs,underscore.js,Javascript,Angularjs,Underscore.js,如何在角度中使用下划线throttle/debounce来计算事件触发的x倍。然后在一定的时间间隔后,用x的知识触发事件 html 正如我在评论中所写,您使用油门每X毫秒调用一个“重载”函数,因此您可以这样做: <div ng-app ng-controller="testC"> <input type="submit" ng-click="add()" value="add"> counter: {{counter}} <br> th

如何在角度中使用下划线throttle/debounce来计算事件触发的x倍。然后在一定的时间间隔后,用x的知识触发事件

html


正如我在评论中所写,您使用油门每X毫秒调用一个“重载”函数,因此您可以这样做:

<div ng-app ng-controller="testC">
    <input type="submit" ng-click="add()" value="add">
    counter: {{counter}} <br>
    throttle: {{ throttle }}<br>
    non-throttle: {{ nonthrottle }}
</div>

function testC($scope, $timeout) {
   $scope.counter = 0;
   $scope.add = function () {
        $scope.counter++;
   }

   $scope.throttle = 0;
   $scope.$watch('counter', _.throttle(function(value) {
        // Wrapping this in '$timeout' will ensure a digest cycle execute that will update the view (Since underscore is not native angular function)
        $timeout(function() {
             $scope.throttle++;
        });
   }, 500));

   $scope.nonthrottle = 0;
   $scope.$watch('counter', function(value) {
     $scope.nonthrottle++;
   });
}

计数器:{{counter}}
节流阀:{{节流阀}}
非节流:{{nonthrottle}} 函数testC($scope,$timeout){ $scope.counter=0; $scope.add=函数(){ $scope.counter++; } $scope.throttle=0; $scope.$watch('计数器',u.节流阀(功能(值)){ //将其包装在“$timeout”中将确保执行一个摘要循环来更新视图(因为下划线不是本机角度函数) $timeout(函数(){ $scope.throttle++; }); }, 500)); $scope.nonthrottle=0; $scope.$watch('counter',函数(值){ $scope.nonthrottle++; }); }
你应该快速点击按钮,你会发现油门监视程序不会在每次点击按钮时更新,但最多每500毫秒更新一次


工作示例:

\节流阀
不是用于每x次触发一次,而是用于每x毫秒最多触发一次,但假设函数add有一个计数器和对节流阀中函数的调用。那就行了。但是我怎么用angular来写呢?你的意思是:(试着快速点击按钮)问题中应该有代码来解释它,而不用点击JSFIDLE链接。如果没有
示例
代码,表单应该清楚地说,到jsfiddle.net的链接必须附带代码。请不要欺骗消息验证器。我的JSFIDLE示例有什么问题?JSFIDLE只是一个概念证明。我真正的代码有点重而且不相关。谢谢,我用的是类似的东西。但我认为在这种情况下,debounce比throttle好。@PresidentCamacho然后你可以使用
。.debounce
,但不要忘记在更新视图相关属性时插入
$timeout
并包装代码,它将触发摘要循环并更新视图(我已经更新了答案并包含了
$timeout
function testC($scope) {
    $scope.counter = 0;
    $scope.add = function () {
        $scope.counter++;
    }

    /*

        counter x-timed add has triggered
      after interval has been reached trigger a event that does $scope.counter = $scope.counter + x

   */
}
<div ng-app ng-controller="testC">
    <input type="submit" ng-click="add()" value="add">
    counter: {{counter}} <br>
    throttle: {{ throttle }}<br>
    non-throttle: {{ nonthrottle }}
</div>

function testC($scope, $timeout) {
   $scope.counter = 0;
   $scope.add = function () {
        $scope.counter++;
   }

   $scope.throttle = 0;
   $scope.$watch('counter', _.throttle(function(value) {
        // Wrapping this in '$timeout' will ensure a digest cycle execute that will update the view (Since underscore is not native angular function)
        $timeout(function() {
             $scope.throttle++;
        });
   }, 500));

   $scope.nonthrottle = 0;
   $scope.$watch('counter', function(value) {
     $scope.nonthrottle++;
   });
}