index.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /**
  2. * 定时器管理类
  3. */
  4. import {
  5. EncryptManager
  6. } from "../encrypt/index.js"
  7. export class TimerManager {
  8. /**
  9. * @param timeStep 时间间隔(默认为1000)
  10. */
  11. constructor(timeStep = 1000) {
  12. // 时间间隔(毫秒)
  13. this._timeStep = timeStep;
  14. // 是否运行
  15. this._isRunning = false;
  16. // 是否暂停
  17. this._isPaused = false;
  18. // 定时器id
  19. this._timer = null;
  20. // 执行方法对象
  21. this._funcList = {};
  22. // 加密类
  23. this.encryptManager = new EncryptManager();
  24. }
  25. /**
  26. * 添加
  27. * @param 执行方法
  28. */
  29. add = (func, name) => {
  30. if (!name) {
  31. name = this.encryptManager.md5(Date.parse(new Date()))
  32. }
  33. // this._funcList.push(func);
  34. this._funcList[name] = func;
  35. if (this._isRunning || this._isPaused) return;
  36. this.run();
  37. }
  38. /**
  39. * 开始
  40. */
  41. start = () => {
  42. this._isPaused = false;
  43. this.run();
  44. }
  45. /**
  46. * 运行
  47. */
  48. run = () => {
  49. this._timer && clearTimeout(this._timer);
  50. this._timer = null;
  51. if (this._isPaused) return;
  52. let funcListValues = Object.values(this._funcList);
  53. if (funcListValues.length == 0) {
  54. this._isRunning = false;
  55. return;
  56. }
  57. this._isRunning = true;
  58. let funcListNames = Object.keys(this._funcList);
  59. // 遍历方法列表执行添加的方法
  60. for (let i = 0; i < funcListValues.length; i++) {
  61. let func = funcListValues[i];
  62. // console.log(name);
  63. // 如果有返回值为false则删除
  64. if (func() === false) {
  65. let name = funcListNames[i];
  66. delete this._funcList[name];
  67. }
  68. func = null;
  69. }
  70. this._timer = setTimeout(this.run, this._timeStep);
  71. }
  72. /**
  73. * 暂停
  74. */
  75. stop = () => {
  76. this._isPaused = true;
  77. }
  78. /**
  79. * 清除所有定时器
  80. */
  81. clear = () => {
  82. this._timer && clearTimeout(this._timer);
  83. this._timer = null;
  84. this._isRunning = false;
  85. this._isPaused = false;
  86. // this._funcList = [];
  87. this._funcList = {};
  88. }
  89. /**
  90. * 移除一个定时器任务
  91. */
  92. remove = (name) => {
  93. if (this._funcList.hasOwnProperty(name)) {
  94. delete this._funcList[name];
  95. }
  96. }
  97. /**
  98. * 设置一个定时器
  99. */
  100. setTimer = ({
  101. name,
  102. seconds,
  103. onChange,
  104. onComplate
  105. } = params) => {
  106. // 移除已经设置的当前定时器
  107. this.remove(name);
  108. let count = 0;
  109. this.add(() => {
  110. count++;
  111. if (count >= seconds) {
  112. this.remove(name);
  113. if (onComplate && typeof onComplate === "function") {
  114. onComplate(true)
  115. }
  116. } else {
  117. if (onChange && typeof onChange === "function") {
  118. onChange({
  119. outSeconds: count,
  120. remainSeconds: seconds - count
  121. });
  122. }
  123. }
  124. }, name)
  125. }
  126. }