timerUtil.ts 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. export interface IntervalTimerNames {
  2. quote: string; // 行情心跳定时器
  3. trade: string; // 交易心跳定时器
  4. tokenCheck: string; //token校验定时器
  5. quoteDay: string; // 定时轮休盘面
  6. pollingNotice: string; //通知公告轮询
  7. overtimeInterval: string; // 超时,如果太久没有操作界面,则退出登录
  8. accountStauts: string; // 账号状态
  9. spotTrade: string; // 仓单报价列表
  10. buyAndSellMartet: string; // 买卖大厅
  11. buyMarket: string; // 买大厅
  12. sellMarket: string; // 卖大厅
  13. realTime: string; // 实时敞口监控
  14. countdown: string; // 倒计时
  15. quoteSucribe: string; // 行情订阅
  16. }
  17. export interface TimeoutTimerNames {
  18. logoutTimer: string; //登出1s延时器
  19. loadMylieList: string; //发布闲置之后延时请求接口数据 不然马上新增数据马上请求是请求不到的
  20. debounce: string; // 防抖
  21. debounceInput: string; // 输入框防抖
  22. filterTimer: string;
  23. debounceOnSearch: string; // 搜索框防抖
  24. overtimeOut: string; // 超时,如果太久没有操作界面,则退出登录
  25. subscribeQuote: string; //按需订阅防抖
  26. }
  27. class TimerUtils {
  28. private timeOutMap;
  29. private intervalMap;
  30. constructor() {
  31. this.timeOutMap = new Map();
  32. this.intervalMap = new Map();
  33. }
  34. /**
  35. * 延迟执行函数
  36. * @param callback 制定函数
  37. * @param delay 延迟毫秒数
  38. * @param code 指定别名
  39. */
  40. public setTimeout(callback: () => void, delay: number, code: keyof TimeoutTimerNames): void {
  41. if (Boolean(callback) && Boolean(delay)) {
  42. let timeoutId = 0;
  43. if (code) {
  44. timeoutId = window.setTimeout(callback, delay);
  45. this.timeOutMap.set(code, timeoutId);
  46. }
  47. }
  48. }
  49. /**
  50. * 清除指定timeOut
  51. * @param code 指定ID
  52. */
  53. public clearTimeout(code: keyof TimeoutTimerNames): void {
  54. if (Boolean(code) && this.timeOutMap.has(code)) {
  55. window.clearTimeout(this.timeOutMap.get(code));
  56. this.timeOutMap.delete(code);
  57. }
  58. }
  59. /**
  60. * 指定周期执行
  61. * @param callback 指定回调函数
  62. * @param delay 间隔执行毫秒数
  63. * @param code 指定别名
  64. */
  65. public setInterval(callback: () => void, delay: number, code: keyof IntervalTimerNames): void {
  66. if (Boolean(callback) && Boolean(delay)) {
  67. let timeoutId = 0;
  68. if (code) {
  69. this.clearInterval(code);
  70. timeoutId = window.setInterval(callback, delay);
  71. this.intervalMap.set(code, timeoutId);
  72. }
  73. }
  74. }
  75. /**
  76. * 清除指定Interval
  77. * @param code 指定ID
  78. */
  79. public clearInterval(code: keyof IntervalTimerNames) {
  80. if (Boolean(code) && this.intervalMap.has(code)) {
  81. window.clearInterval(this.intervalMap.get(code));
  82. this.intervalMap.delete(code);
  83. }
  84. }
  85. /**
  86. * 清除所有Interval
  87. */
  88. public clearAll() {
  89. const intervalList = Array.from(this.intervalMap);
  90. const timeoutList = Array.from(this.timeOutMap);
  91. intervalList.forEach((item) => {
  92. window.clearInterval(item[1]);
  93. });
  94. timeoutList.forEach((item) => {
  95. window.clearTimeout(item[1]);
  96. });
  97. }
  98. }
  99. const timerUtil = new TimerUtils();
  100. export default timerUtil;