Skip to content

debounceEarly

Subhajit Sahu edited this page Aug 7, 2022 · 1 revision

Generate leading-edge debounced version of a function.

Alternatives: debounce, debounceEarly.
Similar: throttle, debounce.


function debounceEarly(x, t, T)
// x: a function
// t: delay time (ms)
// T: max delay time [-1 ⇒ none]
const xasyncfn = require('extra-async-function');


var count = 0;
var fn = xasyncfn.debounceEarly(() => ++count, 1500);
setTimeout(fn, 0);
setTimeout(fn, 1000);
setTimeout(fn, 2000);
setTimeout(fn, 4000);
setTimeout(fn, 5000);
// `count` incremented after 0s
// `count` incremented after 4s


var count = 0;
var fn = xasyncfn.debounceEarly(() => ++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 0s
// `count` incremented after 4s


var count = 0;
var fn = xasyncfn.debounceEarly(() => ++count, 1500, 3500);
setTimeout(fn, 0);
setTimeout(fn, 1000);
setTimeout(fn, 2000);
setTimeout(fn, 3000);
setTimeout(fn, 4000);
setTimeout(fn, 6000);
// `count` incremented after 0s
// `count` incremented after 4s
// `count` incremented after 6s


References

Clone this wiki locally