dio-v
8/27/2014 - 9:45 AM

throttle2 $('body').on('mousemove', throttle2(function (event) { console.log('tick'); }, 1000));

throttle2 $('body').on('mousemove', throttle2(function (event) { console.log('tick'); }, 1000));

function throttle2(fn, threshhold, scope) {
  threshhold || (threshhold = 250);
  var last,
      deferTimer;
  return function () {
    var context = scope || this;

    var now = +new Date,
        args = arguments;
    if (last && now < last + threshhold) {
      // hold on to it
      clearTimeout(deferTimer);
      deferTimer = setTimeout(function () {
        last = now;
        fn.apply(context, args);
      }, threshhold);
    } else {
      last = now;
      fn.apply(context, args);
    }
  };
}