Design & Download
Your JS Library
in Seconds!!!

Snippet: debounce()

Written by Christopher West (cwest) on November 29, 2015.
Creates a copy of a function which will not execute until a specified amount of time has passed since the previous call to the function was made.
function debounce(fn, msBetweenCalls, opt_immediate) {
  var timeoutID,
  return function() {
    var objThis = this,
        args = arguments;
    if (opt_immediate && !timeoutID) {
    timeoutID = setTimeout(fnDebounced, msBetweenCalls);
    function fnDebounced(oldTimeoutID) {
      timeoutID = 0;
      if (!opt_immediate) {
        result = fn.apply(objThis, args);
    return result;

debounce() API Documentation


Creates a debounced copy of the function which when called will delay the execution until the specified amount of milliseconds have elapsed since the last time it ran.


  1. fnCallback {Function}:
    The function to debounce. The context and parameters sent to the debounced function will be sent to this function.
  2. delay {Number}:
    The amount of milliseconds that must pass since the last call to this function before fnCallback will be invoked.
  3. opt_immediate {Boolean}:
    Optional. Defaults to false. Specify true if the debounce function should run immediately instead of initially waiting for the delay. If true is specified, when the debounced function runs, if the delay has elapsed, the value returned from the debounced function will be that which was returned by fnCallback.


The debounced copy of fnCallback.