最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Can I pass parameters to a function debounced with _.lodash? - Stack Overflow

programmeradmin1浏览0评论

I have been trying to use _lodash.debounce() and i have it working. 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. 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. Notice that on the 4th line i get the value of $scope.option.SPidRange and use that in the function. 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

I would like to pass this value of pid into the debounced function but i am not sure how to do this. 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() ?

I have been trying to use _lodash.debounce() and i have it working. 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. 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. Notice that on the 4th line i get the value of $scope.option.SPidRange and use that in the function. 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

I would like to pass this value of pid into the debounced function but i am not sure how to do this. 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() ?

Share Improve this question edited Jan 14, 2014 at 10:12 Panos Bariamis 4,6536 gold badges39 silver badges62 bronze badges asked Jan 14, 2014 at 10:03 user1943020user1943020 1
  • What are the $ signs? – mikemaccana Commented May 15, 2017 at 16:39
Add a comment  | 

1 Answer 1

Reset to default 13

UPDATE

You should pass an argument into the function: _.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

An example with $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);
};
发布评论

评论列表(0)

  1. 暂无评论