Javascript lodash节流功能不起作用';好像没人叫你
我第一次使用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
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)
啊!谢谢你指出这一点!我读过的一些指南并没有明确指出我所看到的。帮个大忙!啊!!谢谢你指出这一点!我读过的一些指南并没有明确指出我所看到的。帮个大忙!