Skip to content

throttle

Subhajit Sahu edited this page Jul 22, 2022 · 2 revisions

Generate throttled version of a function.

Alternatives: throttle, [throttleEarly].
Similar: throttle, [debounce].


function throttle(x, t)
// x: a function
// t: wait time (ms)
const {throttle} = require('extra-function');


var count = 0;
var fn = throttle(() => ++count, 2500);
setTimeout(fn, 0);
setTimeout(fn, 1000);
setTimeout(fn, 2000);
// `count` incremented after 2.5s


var count = 0;
var fn = throttle(() => ++count, 2500);
setTimeout(fn, 0);
setTimeout(fn, 1000);
setTimeout(fn, 2000);
setTimeout(fn, 3000);
setTimeout(fn, 4000);
// `count` incremented after 2.5s
// `count` incremented after 5.5s


References

Clone this wiki locally