-
Notifications
You must be signed in to change notification settings - Fork 0
debounce
Subhajit Sahu edited this page Jul 22, 2022
·
2 revisions
Generate debounced version of a function.
Alternatives: debounce, [debounceEarly].
Similar: [throttle], debounce.
function debounce(x, t, T)
// x: a function
// t: delay time (ms)
// T: max delay time [-1 ⇒ none]
const {debounce} = require('extra-function');
var count = 0;
var fn = debounce(() => ++count, 1500);
setTimeout(fn, 0);
setTimeout(fn, 1000);
setTimeout(fn, 2000);
setTimeout(fn, 4000);
setTimeout(fn, 5000);
// `count` incremented after 3.5s
// `count` incremented after 6.5s
var count = 0;
var fn = debounce(() => ++count, 1500);
setTimeout(fn, 0);
setTimeout(fn, 500);
setTimeout(fn, 1000);
setTimeout(fn, 1500);
setTimeout(fn, 2000);
setTimeout(fn, 4000);
setTimeout(fn, 4500);
setTimeout(fn, 5000);
// `count` incremented after 3.5s
// `count` incremented after 6.5s
var count = 0;
var fn = debounce(() => ++count, 1500, 3500);
setTimeout(fn, 0);
setTimeout(fn, 1000);
setTimeout(fn, 2000);
setTimeout(fn, 3000);
setTimeout(fn, 4000);
setTimeout(fn, 6000);
// `count` incremented after 3.5s
// `count` incremented after 5.5s
// `count` incremented after 7.5s