It throttles all the request that come up in the specified interval after invoking the first one (which prints 1
) and Subsequent calls only return the result of the last invocation (which prints 5
) after the interval is over. So, If you just do something like this, you'll see each of the values getting printed
const f = function (a) {
console.log(a);
};
let f1000 = _.throttle(f, 1000);
setTimeout(()=>f1000(1), 1000)
setTimeout(()=>f1000(2), 2000)
setTimeout(()=>f1000(3), 3000)
setTimeout(()=>f1000(4), 4000)
setTimeout(()=>f1000(5), 5000)
Or, if you just reduce the throttle time to 0 milliseconds, it will print all the values.
const f = function (a) {
console.log(a);
};
let f1000 = _.throttle(f, 0); //reduce throttle to 0
f1000(1);
f1000(2);
f1000(3);
f1000(4);
f1000(5);
I got this clue when I put a debugger in the function and kept the dev tool open. In that case it was printing each of the numbers (since, there was sufficient delay against each invocation). You can see this for yourself too.
const f = function (a) {
debugger;
console.log(a);
};
Also, you might like to read the doc: https://lodash.com/docs/4.17.15#throttle