index.ts 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. import { v4 } from '../uuid/index'
  2. /**
  3. * 定时任务
  4. */
  5. export const timerTask = new (class {
  6. private timeoutMap = new Map<string, number>();
  7. private intervalMap = new Map<string, number>();
  8. /**
  9. * 设置定时器
  10. * @param callback
  11. * @param delay
  12. * @param timerId
  13. */
  14. setTimeout(callback: () => void, delay = 100, timerId?: string) {
  15. const uuid = timerId || v4();
  16. this.clearTimeout(uuid);
  17. const timer = setTimeout(() => {
  18. this.timeoutMap.delete(uuid);
  19. callback();
  20. }, delay)
  21. this.timeoutMap.set(uuid, timer);
  22. return {
  23. uuid,
  24. clear: () => this.clearTimeout(uuid)
  25. }
  26. }
  27. /**
  28. * 设置周期定时器
  29. * @param callback
  30. * @param delay
  31. * @param timerId
  32. */
  33. setInterval(callback: () => void, delay = 100, timerId?: string) {
  34. const uuid = timerId || v4();
  35. this.clearInterval(uuid);
  36. const timer = setInterval(() => {
  37. callback();
  38. }, delay)
  39. this.intervalMap.set(uuid, timer);
  40. return {
  41. uuid,
  42. clear: () => this.clearInterval(uuid)
  43. }
  44. }
  45. /**
  46. * 清除定时器
  47. * @param keys
  48. */
  49. clearTimeout(...keys: string[]) {
  50. if (keys.length) {
  51. keys.forEach((key) => {
  52. if (this.timeoutMap.has(key)) {
  53. clearTimeout(this.timeoutMap.get(key));
  54. this.timeoutMap.delete(key);
  55. }
  56. })
  57. } else {
  58. for (const [key, value] of this.timeoutMap.entries()) {
  59. clearTimeout(value);
  60. this.timeoutMap.delete(key);
  61. }
  62. }
  63. }
  64. /**
  65. * 清除周期定时器
  66. * @param keys
  67. */
  68. clearInterval(...keys: string[]) {
  69. if (keys.length) {
  70. keys.forEach((key) => {
  71. if (this.intervalMap.has(key)) {
  72. clearInterval(this.intervalMap.get(key));
  73. this.intervalMap.delete(key);
  74. }
  75. })
  76. } else {
  77. for (const [key, value] of this.intervalMap.entries()) {
  78. clearInterval(value);
  79. this.intervalMap.delete(key);
  80. }
  81. }
  82. }
  83. /**
  84. * 清除所有定时器
  85. */
  86. clearAll() {
  87. this.clearTimeout();
  88. this.clearInterval();
  89. }
  90. })
  91. /**
  92. * 定时拦截器
  93. */
  94. export const timerInterceptor = new (class {
  95. private debounceMap = new Map<string, number>();
  96. private throttleMap = new Map<string, number>();
  97. /**
  98. * 函数防抖(等待触发)
  99. * @param callback 回调函数
  100. * @param delay 延迟毫秒数,默认100毫秒
  101. * @returns
  102. */
  103. setDebounce<T extends unknown[]>(callback: (...params: T) => void, delay = 100) {
  104. let timer = 0;
  105. return function (...args: T) {
  106. clearTimeout(timer);
  107. timer = setTimeout(() => {
  108. callback(...args);
  109. }, delay)
  110. }
  111. }
  112. /**
  113. * 函数节流(间隔触发)
  114. * @param callback 回调函数
  115. * @param delay 延迟毫秒数,默认100毫秒
  116. * @returns
  117. */
  118. setThrottle<T extends unknown[]>(callback: (...params: T) => void, delay = 100) {
  119. let timer = 0;
  120. return function (...args: T) {
  121. if (timer === 0) {
  122. timer = setTimeout(() => {
  123. timer = 0;
  124. callback(...args);
  125. }, delay)
  126. }
  127. }
  128. }
  129. /**
  130. * 函数防抖(等待触发)
  131. * @param callback 回调函数
  132. * @param delay 延迟毫秒数,默认100毫秒
  133. * @param timerId 计时器ID
  134. */
  135. debounce(callback: () => void, delay = 100, timerId = 'timer') {
  136. if (this.debounceMap.has(timerId)) {
  137. const t = this.debounceMap.get(timerId);
  138. clearTimeout(t);
  139. }
  140. const timer = setTimeout(() => {
  141. this.debounceMap.delete(timerId);
  142. callback();
  143. }, delay)
  144. this.debounceMap.set(timerId, timer);
  145. }
  146. /**
  147. * 函数节流(间隔触发)
  148. * @param callback 回调函数
  149. * @param delay 延迟毫秒数,默认100毫秒
  150. * @param timerId 计时器ID
  151. * @returns
  152. */
  153. throttle(callback: () => void, delay = 100, timerId = 'timer') {
  154. if (this.throttleMap.has(timerId)) {
  155. const t = this.throttleMap.get(timerId);
  156. if (t) return;
  157. }
  158. let timer = setTimeout(() => {
  159. timer = 0;
  160. this.throttleMap.delete(timerId);
  161. callback();
  162. }, delay)
  163. this.throttleMap.set(timerId, timer);
  164. }
  165. })