debounce window.resize

by Cristi Salcescu

HTML

<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3/underscore.js"></script>

JavaScript

function debounce(fn, interval) {
  let timer;
  return function debounced(...args) {
    clearTimeout(timer);
    timer = setTimeout(function call() {
      fn(...args);
    }, interval);
  };
}

function process(){ console.log("process"); }

let debouncedProcess = debounce(process, 400);
$(window).resize(debouncedProcess);