debounce.js 405 B

123456789101112131415
  1. exports = function(fn, wait, immediate) {
  2. var timeout;
  3. return function() {
  4. var ctx = this;
  5. var args = arguments;
  6. var throttler = function() {
  7. timeout = null;
  8. fn.apply(ctx, args);
  9. };
  10. if (!immediate) clearTimeout(timeout);
  11. if (!immediate || !timeout) timeout = setTimeout(throttler, wait);
  12. };
  13. };
  14. module.exports = exports;