javascript 我可以将参数传递给使用 _.lodash 去抖动的函数吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21110656/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Can I pass parameters to a function debounced with _.lodash?
提问by
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:
我一直在尝试使用 _lodash.debounce() 并且我让它工作了。但是我不确定它是否以最好的方式工作。我查看了 lodash 网站上的示例,它们似乎只是不传递参数的简单示例。这是我所拥有的:
$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.
上面的代码返回一个$scope.parsePid
去抖动的函数。请注意,在第 4 行,我获取了 的值$scope.option.SPidRange
并在函数中使用了它。我真的很想以某种方式传递这个参数而不是这样得到它。
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. I tried a few different things but the debounce function gives an error.
我想将此 pid 值传递到去抖动函数中,但我不确定如何执行此操作。我尝试了一些不同的东西,但 debounce 函数给出了一个错误。
Is it possible to pass parameters into the debounced function $scope.parsePid()
?
是否可以将参数传递到 debounced 中function $scope.parsePid()
?
采纳答案by Ilan Frumer
UPDATE
更新
You should pass an argument into the function: _.debounce(function (pid) {
您应该将参数传递给函数: _.debounce(function (pid) {
$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
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);
};