Timeout.js 673 B

123456789101112131415161718192021222324252627282930313233
  1. export default class Timeout {
  2. constructor(e, t, r=!0) {
  3. this._timeout = null
  4. this._fn = e,
  5. this._delay = t,
  6. r && this.start()
  7. }
  8. get delay() {
  9. return this._delay
  10. }
  11. get isSet() {
  12. return !!this._timeout
  13. }
  14. setDelay(e) {
  15. this._delay = e
  16. }
  17. start() {
  18. this.isSet || (this._timeout = window.setTimeout(()=>{
  19. const e = this._fn;
  20. this.clear(),
  21. e()
  22. }
  23. , this._delay))
  24. }
  25. clear() {
  26. window.clearTimeout(this._timeout),
  27. this._timeout = void 0
  28. }
  29. reset() {
  30. this.clear(),
  31. this.start()
  32. }
  33. }