debouncer.js 1.3KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. (function (factory) {
  2. if (typeof module === "object" && typeof module.exports === "object") {
  3. var v = factory(require, exports);
  4. if (v !== undefined) module.exports = v;
  5. }
  6. else if (typeof define === "function" && define.amd) {
  7. define(["require", "exports"], factory);
  8. }
  9. })(function (require, exports) {
  10. "use strict";
  11. Object.defineProperty(exports, "__esModule", { value: true });
  12. var TimeoutDebouncer = (function () {
  13. function TimeoutDebouncer(wait) {
  14. this.wait = wait;
  15. this.timer = null;
  16. }
  17. TimeoutDebouncer.prototype.debounce = function (callback) {
  18. this.callback = callback;
  19. this.schedule();
  20. };
  21. TimeoutDebouncer.prototype.schedule = function () {
  22. this.cancel();
  23. if (this.wait <= 0) {
  24. this.callback();
  25. }
  26. else {
  27. this.timer = setTimeout(this.callback, this.wait);
  28. }
  29. };
  30. TimeoutDebouncer.prototype.cancel = function () {
  31. if (this.timer) {
  32. clearTimeout(this.timer);
  33. this.timer = null;
  34. }
  35. };
  36. return TimeoutDebouncer;
  37. }());
  38. exports.TimeoutDebouncer = TimeoutDebouncer;
  39. });
  40. //# sourceMappingURL=debouncer.js.map