| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186 |
- import { v4 } from '../uuid/index'
- /**
- * 定时任务
- */
- export const timerTask = new (class {
- private timeoutMap = new Map<string, number>();
- private intervalMap = new Map<string, number>();
- /**
- * 设置定时器
- * @param callback
- * @param delay
- * @param timerId
- */
- setTimeout(callback: () => void, delay = 100, timerId?: string) {
- const uuid = timerId || v4();
- this.clearTimeout(uuid);
- const timer = setTimeout(() => {
- this.timeoutMap.delete(uuid);
- callback();
- }, delay)
- this.timeoutMap.set(uuid, timer);
- return {
- uuid,
- clear: () => this.clearTimeout(uuid)
- }
- }
- /**
- * 设置周期定时器
- * @param callback
- * @param delay
- * @param timerId
- */
- setInterval(callback: () => void, delay = 100, timerId?: string) {
- const uuid = timerId || v4();
- this.clearInterval(uuid);
- const timer = setInterval(() => {
- callback();
- }, delay)
- this.intervalMap.set(uuid, timer);
- return {
- uuid,
- clear: () => this.clearInterval(uuid)
- }
- }
- /**
- * 清除定时器
- * @param keys
- */
- clearTimeout(...keys: string[]) {
- if (keys.length) {
- keys.forEach((key) => {
- if (this.timeoutMap.has(key)) {
- clearTimeout(this.timeoutMap.get(key));
- this.timeoutMap.delete(key);
- }
- })
- } else {
- for (const [key, value] of this.timeoutMap.entries()) {
- clearTimeout(value);
- this.timeoutMap.delete(key);
- }
- }
- }
- /**
- * 清除周期定时器
- * @param keys
- */
- clearInterval(...keys: string[]) {
- if (keys.length) {
- keys.forEach((key) => {
- if (this.intervalMap.has(key)) {
- clearInterval(this.intervalMap.get(key));
- this.intervalMap.delete(key);
- }
- })
- } else {
- for (const [key, value] of this.intervalMap.entries()) {
- clearInterval(value);
- this.intervalMap.delete(key);
- }
- }
- }
- /**
- * 清除所有定时器
- */
- clearAll() {
- this.clearTimeout();
- this.clearInterval();
- }
- })
- /**
- * 定时拦截器
- */
- export const timerInterceptor = new (class {
- private debounceMap = new Map<string, number>();
- private throttleMap = new Map<string, number>();
- /**
- * 函数防抖(等待触发)
- * @param callback 回调函数
- * @param delay 延迟毫秒数,默认100毫秒
- * @returns
- */
- setDebounce<T extends unknown[]>(callback: (...params: T) => void, delay = 100) {
- let timer = 0;
- return function (...args: T) {
- clearTimeout(timer);
- timer = setTimeout(() => {
- callback(...args);
- }, delay)
- }
- }
- /**
- * 函数节流(间隔触发)
- * @param callback 回调函数
- * @param delay 延迟毫秒数,默认100毫秒
- * @returns
- */
- setThrottle<T extends unknown[]>(callback: (...params: T) => void, delay = 100) {
- let timer = 0;
- return function (...args: T) {
- if (timer === 0) {
- timer = setTimeout(() => {
- timer = 0;
- callback(...args);
- }, delay)
- }
- }
- }
- /**
- * 函数防抖(等待触发)
- * @param callback 回调函数
- * @param delay 延迟毫秒数,默认100毫秒
- * @param timerId 计时器ID
- */
- debounce(callback: () => void, delay = 100, timerId = 'timer') {
- if (this.debounceMap.has(timerId)) {
- const t = this.debounceMap.get(timerId);
- clearTimeout(t);
- }
- const timer = setTimeout(() => {
- this.debounceMap.delete(timerId);
- callback();
- }, delay)
- this.debounceMap.set(timerId, timer);
- }
- /**
- * 函数节流(间隔触发)
- * @param callback 回调函数
- * @param delay 延迟毫秒数,默认100毫秒
- * @param timerId 计时器ID
- * @returns
- */
- throttle(callback: () => void, delay = 100, timerId = 'timer') {
- if (this.throttleMap.has(timerId)) {
- const t = this.throttleMap.get(timerId);
- if (t) return;
- }
- let timer = setTimeout(() => {
- timer = 0;
- this.throttleMap.delete(timerId);
- callback();
- }, delay)
- this.throttleMap.set(timerId, timer);
- }
- })
|