Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/404.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/api/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript lodash节流功能不起作用';好像没人叫你_Javascript_Api_Lodash_Throttling - Fatal编程技术网

Javascript lodash节流功能不起作用';好像没人叫你

Javascript lodash节流功能不起作用';好像没人叫你,javascript,api,lodash,throttling,Javascript,Api,Lodash,Throttling,我第一次使用lodash的throttle函数尝试限制对API的调用数量,但是在我的尝试中,我似乎无法让它触发调用 我在下面提供了一个简化版本: const _ = require('lodash'); let attempts = 0; const incrementAttempts = () => { console.log('incrementing attempts'); attempts++; console.log("attempts now: &qu

我第一次使用lodash的
throttle
函数尝试限制对API的调用数量,但是在我的尝试中,我似乎无法让它触发调用

我在下面提供了一个简化版本:

const _ = require('lodash');

let attempts = 0;

const incrementAttempts = () => {
  console.log('incrementing attempts');
  attempts++;
  console.log("attempts now: " + attempts);
}

const throttledAttempts = () => { 
  // From my understanding, this will throttle calls to increasedAttempts to one call every 500 ms
  _.throttle(incrementAttempts(), 500); 
}

// We should make 5 attempts at the call
while(attempts < 5) {
  throttledAttempts();
}
查找此错误后,我看到一个建议,建议向
throttle
函数添加匿名包装,因此现在我的
throttledAttempts
如下所示:

const throttledAttempts = () => { 
  _.throttle(() => incrementAttempts(), 500); 
}
然而,这样做。。。我现在得到控制台输出


我做错了什么?

节流阀返回新的节流功能。您的代码应该类似于:

let attempts = 0;

const incrementAttempts = () => {
  console.log('incrementing attempts');
  attempts++;
  console.log("attempts now: " + attempts);
}

const throttledAttempts = _.throttle(incrementAttempts, 500);

// We should make 5 attempts at the call
while(attempts < 5) {
  throttledAttempts();
}
let尝试次数=0;
常量递增尝试=()=>{
log(“递增尝试”);
尝试++;
log(“立即尝试:+尝试”);
}
const throttledAttempts=uu.throttle(递增尝试次数,500);
//我们应该试着打5次电话
while(尝试次数<5次){
throttledAttempts();
}

节流阀返回新的节流功能。您的代码应该类似于:

let attempts = 0;

const incrementAttempts = () => {
  console.log('incrementing attempts');
  attempts++;
  console.log("attempts now: " + attempts);
}

const throttledAttempts = _.throttle(incrementAttempts, 500);

// We should make 5 attempts at the call
while(attempts < 5) {
  throttledAttempts();
}
let尝试次数=0;
常量递增尝试=()=>{
log(“递增尝试”);
尝试++;
log(“立即尝试:+尝试”);
}
const throttledAttempts=uu.throttle(递增尝试次数,500);
//我们应该试着打5次电话
while(尝试次数<5次){
throttledAttempts();
}

incrementAttempts()
调用
incrementAttempts
,并将
未定义的
传递给
.throttle
,因为
incrementAttempts
不会返回任何内容。它应该是
.throttle(incrementAttempts,500)
调用
incrementAttempts()
,并将
未定义的
传递给
节流阀
,因为
递增尝试次数
不会返回任何内容。它应该是
。\throttle(递增尝试次数,500)
啊!谢谢你指出这一点!我读过的一些指南并没有明确指出我所看到的。帮个大忙!啊!!谢谢你指出这一点!我读过的一些指南并没有明确指出我所看到的。帮个大忙!