CommonUtil.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. import { md5_hex } from "./md5"
  2. class _CommonUtil {
  3. functionReqMap = new Map()
  4. setTimeOutMap = new Map()
  5. /**
  6. * 防抖函数(调用会立即触发,在wait时间内不再触发)
  7. * @param fun 业务函数
  8. * @param wait 等待时间(在等待时间内防止重复触发,默认1.5秒内拒绝所有数据)
  9. * 使用示例:wait可以不填写,默认1500毫秒
  10. * CommonUtil.debounce(()=>{
  11. * // todo
  12. * },500)
  13. */
  14. debounce(fun, wait = 1500) {
  15. let funcValue1 = fun.toString()
  16. let hash = md5_hex(funcValue1)
  17. let startTime = Date.now()
  18. if (this.functionReqMap.get(hash)) {
  19. let funcValue = this.functionReqMap.get(hash)
  20. // 防止因为特殊原因没有移除map中该函数的记录,导致一直无法执行函数的问题
  21. if (funcValue && funcValue.startTime + funcValue.wait <= startTime) {
  22. this.functionReqMap.delete(hash)
  23. } else {
  24. return
  25. }
  26. }
  27. this.functionReqMap.set(hash, {
  28. startTime: startTime,
  29. wait: wait
  30. })
  31. // 执行函数调用
  32. fun()
  33. // 拦截在wait期间的函数再次调用,在超时后,将限制解除
  34. setTimeout(() => {
  35. let needRemoveKeys = []
  36. this.functionReqMap.forEach((value, key, map) => {
  37. let curTime = Date.now()
  38. let funcValue = map.get(key)
  39. if (funcValue && funcValue.startTime + funcValue.wait <= curTime) {
  40. // @ts-ignore
  41. needRemoveKeys.push(key)
  42. }
  43. })
  44. needRemoveKeys.map((value) => {
  45. this.functionReqMap.delete(value)
  46. })
  47. }, wait)
  48. }
  49. debounceHold(fun, wait = 1500) {
  50. return this.throttle(fun, wait)
  51. }
  52. uniqueIdMap = new WeakMap()
  53. getUuid() {
  54. return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, function (c) {
  55. var r = (Math.random() * 16) | 0,
  56. v = c == "x" ? r : (r & 0x3) | 0x8
  57. return v.toString(16)
  58. })
  59. }
  60. getUniqueId(fun) {
  61. if (!this.uniqueIdMap.has(fun)) {
  62. this.uniqueIdMap.set(fun, this.getUuid())
  63. }
  64. return this.uniqueIdMap.get(fun)
  65. }
  66. /**
  67. * 防抖动函数,调用后会延迟wait时间执行,当在wait时间内再次对同一函数调用,则会取消之前的定时器,重新定时
  68. * @param fun
  69. * @param wait
  70. * @return setTimeout的句柄
  71. */
  72. throttle(fun, wait = 1500) {
  73. let funcValue1 = this.getUniqueId(fun)
  74. let hash = md5_hex(funcValue1)
  75. if (this.setTimeOutMap.get(hash)) {
  76. clearTimeout(this.setTimeOutMap.get(hash)?.timeoutNumber)
  77. this.setTimeOutMap.delete(hash)
  78. }
  79. // this.checkTimeOutNumber()
  80. let timeoutNumber = setTimeout(() => {
  81. this.setTimeOutMap.get(hash) && clearTimeout(this.setTimeOutMap.get(hash)?.timeoutNumber)
  82. this.setTimeOutMap.delete(hash)
  83. // 执行函数调用
  84. fun()
  85. }, wait)
  86. this.setTimeOutMap.set(hash, {
  87. timeoutNumber: timeoutNumber,
  88. startTime: new Date().getTime()
  89. })
  90. return timeoutNumber
  91. }
  92. /**
  93. * 取消延迟执行函数
  94. * @param timeoutId debounceHold返回的timeout句柄
  95. */
  96. cancel(timeoutId) {
  97. clearTimeout(timeoutId)
  98. }
  99. }
  100. /* eslint-enable */
  101. export const CommonUtil = new _CommonUtil()