Anonymous Anonymous - 11 months ago
323 0

No description

Javascript

Simple debouncer

function debouncer() {
  var callable;
  var tm;
  var args;

  return function(cb, argsArray, delay) {
    console.log('to debounce:', argsArray, delay);
    args = argsArray;
    callable = function() {
      clearInterval(tm);
      tm = null;
      console.log('debounced:', args);
      cb.apply(null, args);
    }
    if (!tm) {
      console.log('queued:', argsArray, delay);
      tm = setTimeout(callable, delay);
    }
  }
}

var toDebounce = function(i) {
  console.log('cb:', i);
}

var delay = 500;
var debounce = debouncer();
var tot = 0;

var runDebounce = function() {
  if (tot<10000) {
    tot++;
    setTimeout(function() { debounce(toDebounce, [tot], delay) }, 0);
    setTimeout(runDebounce, Math.floor(Math.random()*(delay/10)));
  }
}

runDebounce();
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download