Skip to content

Instantly share code, notes, and snippets.

@Aaron2Ti
Forked from tommmyy/ramdaDebounce.js
Created December 14, 2018 16:52
Show Gist options
  • Save Aaron2Ti/1f4cfd921eea2838ed2afda5908e6ed4 to your computer and use it in GitHub Desktop.
Save Aaron2Ti/1f4cfd921eea2838ed2afda5908e6ed4 to your computer and use it in GitHub Desktop.
Debounce function using Ramda.js
import { curry, apply } from 'ramda';
/**
* Debounce
*
* @param {Boolean} immediate If true run `fn` at the start of the timeout
* @param timeMs {Number} Debounce timeout
* @param fn {Function} Function to debounce
*
* @return {Number} timeout
* @example
*
* const say = (x) => console.log(x)
* const debouncedSay = debounce_(false, 1000, say)();
*
* debouncedSay("1")
* debouncedSay("2")
* debouncedSay("3")
*
*/
const debounce_ = curry((immediate, timeMs, fn) => () => {
let timeout;
return (...args) => {
const later = () => {
timeout = null;
if (!immediate) {
apply(fn, args);
}
};
const callNow = immediate && !timeout;
clearTimeout(timeout);
timeout = setTimeout(later, timeMs);
if (callNow) {
apply(fn, args);
}
return timeout;
};
});
export const debounceImmediate = debounce_(true);
export const debounce = debounce_(false);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment