Getting Started with JavaScript for Web Development: Lesson 17 of 23

Scroll Progress

Up Next

Floating Input

Autoplaying in 7 seconds!

Cancel

Debounce Function

function debounce(func, wait = 15, 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);
  };
}

Chris Sevilleja

151 posts

Co-founder of Scotch.io. Slapping the keyboard until something good happens.