简体   繁体   English

我可以将参数传递给使用_.lodash去抖动的函数吗?

[英]Can I pass parameters to a function debounced with _.lodash?

I have been trying to use _lodash.debounce() and i have it working. 我一直在尝试使用_lodash.debounce(),我让它工作。 However i am not sure if it's working the best way it could be. 但是我不确定它是否以最好的方式工作。 I looked at the example on the lodash web site and they seem to be just simple examples that don't pass around parameters. 我查看了lodash网站上的示例,它们似乎只是简单的示例,不会传递参数。 Here's what i have: 这就是我所拥有的:

$scope.parsePid = _.debounce(function () {
    $scope.$apply(function () {
        var pid = $scope.option.sPidRange;
        if (pid == null || pid === "") {
            $scope.pidLower = null;
            $scope.pidUpper = null;
        }
        else if (pid.indexOf("-") > 0) {
            pid = pid.split("-");
            $scope.pidLower = parseInt(pid[0]);
            $scope.pidUpper = parseInt(pid[1]);
        }
        else {
            $scope.pidLower = parseInt(pid);
            $scope.pidUpper = null;
        }
    });
}, 1500);

The above code returns a function $scope.parsePid that's debounced. 上面的代码返回一个被去抖动的函数$scope.parsePid Notice that on the 4th line i get the value of $scope.option.SPidRange and use that in the function. 请注意,在第4行,我得到$scope.option.SPidRange的值,并在函数中使用它。 I really want to somehow pass in this parameter rather than get it this way. 我真的想以某种方式传递这个参数而不是这样做。

I call the function like this: 我把这个函数称为:

$scope.$watch("option.sPidRange", function (pid) {
    if (pid !== null) {
        $scope.parsePid();
    }
});

Here the value pid should be equal to $scope.parsePid 这里的值pid应该等于$scope.parsePid

I would like to pass this value of pid into the debounced function but i am not sure how to do this. 我想将这个pid的值传递给debounced函数,但我不知道该如何做到这一点。 I tried a few different things but the debounce function gives an error. 我尝试了一些不同的东西,但去抖功能会出错。

Is it possible to pass parameters into the debounced function $scope.parsePid() ? 是否可以将参数传递给去抖动function $scope.parsePid()

UPDATE UPDATE

You should pass an argument into the function: _.debounce(function (pid) { 你应该将一个参数传递给函数: _.debounce(function (pid) {

An example with debounce 去抖动的一个例子

$scope.parsePid = _.debounce(function(pid){
  $scope.$apply(function(){
    if (pid === null || pid === "") {
      $scope.pidLower = null;
      $scope.pidUpper = null;
    } else if (pid.indexOf("-") > 0) {
      pid = pid.split("-");
      $scope.pidLower = parseInt(pid[0],10);
      $scope.pidUpper = parseInt(pid[1],10);      
    } else {
      $scope.pidLower = parseInt(pid,10);
      $scope.pidUpper = null;
    }      
  });
},1500);

I would use the built-in $timeout 我会使用内置的$ timeout

An example with $timeout $ timeout的示例

var promise;

$scope.parsePid = function(pid){
  $timeout.cancel(promise);
  promise = $timeout(function(){     
    if (pid === null || pid === "") {
      $scope.pidLower = null;
      $scope.pidUpper = null;
    } else if (pid.indexOf("-") > 0) {
      pid = pid.split("-");
      $scope.pidLower = parseInt(pid[0],10);
      $scope.pidUpper = parseInt(pid[1],10);      
    } else {
      $scope.pidLower = parseInt(pid,10);
      $scope.pidUpper = null;
    }
  },1500);
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 Lodash 取消 function 内部的去抖? - How to cancel debounced inside function with Lodash? 有没有一种方法可以将事件对象以及其他参数传递给去抖动的事件处理程序? - Is there a way I can pass event object as well as other arguments to debounced event handler? 我可以将比较作为 function 参数传递吗? - Can I pass a comparison as a function parameters? 我可以将一个函数的参数(req、res、next)传递给另一个函数吗? - Can I pass the parameters (req, res, next) of a function to another function? 如何在不使用函数参数的情况下将值传递给函数? - How can I pass a value into a function without using function parameters? 我无法使用addEventListener Javascript / Jquery将参数传递给函数 - I can not pass parameters to a function using addEventListener Javascript/Jquery 我可以将搜索框值作为参数传递给函数吗? - Can I pass my search box values as parameters in my function? js sort()自定义函数如何传递更多参数? - js sort() custom function how can i pass more parameters? 如何将对象参数传递给反引号中的onclick函数 - How can I pass object parameters to the onclick function in backtick 我如何将参数传递给函数以获取当前样式 - how can i pass a parameters to a function to get current Style
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM