export interface IntervalTimerNames { quote: string; // 行情心跳定时器 trade: string; // 交易心跳定时器 tokenCheck: string; //token校验定时器 quoteDay: string; // 定时轮休盘面 pollingNotice: string; //通知公告轮询 overtimeInterval: string; // 超时,如果太久没有操作界面,则退出登录 accountStauts: string; // 账号状态 spotTrade: string; // 仓单报价列表 buyAndSellMartet: string; // 买卖大厅 buyMarket: string; // 买大厅 sellMarket: string; // 卖大厅 realTime: string; // 实时敞口监控 countdown: string; // 倒计时 quoteSucribe: string; // 行情订阅 } export interface TimeoutTimerNames { logoutTimer: string; //登出1s延时器 loadMylieList: string; //发布闲置之后延时请求接口数据 不然马上新增数据马上请求是请求不到的 debounce: string; // 防抖 debounceInput: string; // 输入框防抖 filterTimer: string; debounceOnSearch: string; // 搜索框防抖 overtimeOut: string; // 超时,如果太久没有操作界面,则退出登录 subscribeQuote: string; //按需订阅防抖 } class TimerUtils { private timeOutMap; private intervalMap; constructor() { this.timeOutMap = new Map(); this.intervalMap = new Map(); } /** * 延迟执行函数 * @param callback 制定函数 * @param delay 延迟毫秒数 * @param code 指定别名 */ public setTimeout(callback: () => void, delay: number, code: keyof TimeoutTimerNames): void { if (Boolean(callback) && Boolean(delay)) { let timeoutId = 0; if (code) { timeoutId = window.setTimeout(callback, delay); this.timeOutMap.set(code, timeoutId); } } } /** * 清除指定timeOut * @param code 指定ID */ public clearTimeout(code: keyof TimeoutTimerNames): void { if (Boolean(code) && this.timeOutMap.has(code)) { window.clearTimeout(this.timeOutMap.get(code)); this.timeOutMap.delete(code); } } /** * 指定周期执行 * @param callback 指定回调函数 * @param delay 间隔执行毫秒数 * @param code 指定别名 */ public setInterval(callback: () => void, delay: number, code: keyof IntervalTimerNames): void { if (Boolean(callback) && Boolean(delay)) { let timeoutId = 0; if (code) { this.clearInterval(code); timeoutId = window.setInterval(callback, delay); this.intervalMap.set(code, timeoutId); } } } /** * 清除指定Interval * @param code 指定ID */ public clearInterval(code: keyof IntervalTimerNames) { if (Boolean(code) && this.intervalMap.has(code)) { window.clearInterval(this.intervalMap.get(code)); this.intervalMap.delete(code); } } /** * 清除所有Interval */ public clearAll() { const intervalList = Array.from(this.intervalMap); const timeoutList = Array.from(this.timeOutMap); intervalList.forEach((item) => { window.clearInterval(item[1]); }); timeoutList.forEach((item) => { window.clearTimeout(item[1]); }); } } const timerUtil = new TimerUtils(); export default timerUtil;