Free eBook: Build Your First Node App

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

Scroll Progress

Up Next

Floating Input

Autoplaying in 7 seconds!

Cancel

Table of Contents

    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

    173 posts

    Founder of Scotch.io. Google Developer Expert in Web Technologies. Slapping the keyboard until something good happens.