Skip to content

Instantly share code, notes, and snippets.

@zeshanshani
Last active August 25, 2019 19:56
Show Gist options
  • Select an option

  • Save zeshanshani/c480025dc370fceb81c7b86cb3906e8b to your computer and use it in GitHub Desktop.

Select an option

Save zeshanshani/c480025dc370fceb81c7b86cb3906e8b to your computer and use it in GitHub Desktop.

Revisions

  1. zeshanshani revised this gist Aug 25, 2019. 1 changed file with 14 additions and 13 deletions.
    27 changes: 14 additions & 13 deletions debounce.js
    Original file line number Diff line number Diff line change
    @@ -2,17 +2,18 @@
    // be triggered. The function will be called after it stops being called for
    // N milliseconds. If `immediate` is passed, trigger the function on the
    // leading edge, instead of the trailing.
    function debounce(func, wait, immediate) {
    var timeout;
    return function() {
    var context = this, args = arguments;
    var later = function() {
    timeout = null;
    if (!immediate) func.apply(context, args);
    };
    var callNow = immediate && !timeout;
    clearTimeout(timeout);
    timeout = setTimeout(later, wait);
    if (callNow) func.apply(context, args);
    };
    function debounce( func, wait, immediate ) {
    var timeout;
    return function () {
    var context = this,
    args = arguments;
    var later = function () {
    timeout = null;
    if ( !immediate ) func.apply( context, args );
    };
    var callNow = immediate && !timeout;
    clearTimeout( timeout );
    timeout = setTimeout( later, wait );
    if ( callNow ) func.apply( context, args );
    };
    };
  2. zeshanshani revised this gist May 10, 2019. No changes.
  3. zeshanshani revised this gist May 4, 2019. No changes.
  4. zeshanshani revised this gist May 4, 2019. No changes.
  5. zeshanshani created this gist May 4, 2019.
    18 changes: 18 additions & 0 deletions debounce.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,18 @@
    // Returns a function, that, as long as it continues to be invoked, will not
    // be triggered. The function will be called after it stops being called for
    // N milliseconds. If `immediate` is passed, trigger the function on the
    // leading edge, instead of the trailing.
    function debounce(func, wait, immediate) {
    var timeout;
    return function() {
    var context = this, args = arguments;
    var later = function() {
    timeout = null;
    if (!immediate) func.apply(context, args);
    };
    var callNow = immediate && !timeout;
    clearTimeout(timeout);
    timeout = setTimeout(later, wait);
    if (callNow) func.apply(context, args);
    };
    };