throttle()

by Cristi Salcescu

HTML

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

JavaScript

function throttle(fn, interval) {
  let lastTime;
  return function throttled(...args) {
    if(!lastTime || (Date.now() - lastTime >= interval)) {
      fn(...args);
      lastTime = Date.now();
    }
  };
}

function process(){ console.log("process"); }
let throttledProcess = throttle(process, 1000);

$(window).mousemove(throttledProcess);