CountDownSchedule.ts 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. import { Component, director, macro } from "cc";
  2. export default class CountDownSchedule extends Component {
  3. private _lastUpdate = null;
  4. private _observers = null;
  5. public constructor() {
  6. super();
  7. this._observers = [];
  8. this._lastUpdate = (new Date()).getTime();
  9. };
  10. /**
  11. * 启动一个计时器
  12. * @param callback 每次回调
  13. * @param interval 执行间隔
  14. * @param key 计时器标识,用于移除使用
  15. * @example
  16. * <pre>
  17. * private const = 0
  18. * Global.cdSchedule.schedule(() => {
  19. * this.const++;
  20. * console.log(this.const);
  21. * if (this.const == 10) {
  22. * Global.cdSchedule.unschedule("test");
  23. * }
  24. * }, 1, "test");
  25. * </pre>
  26. */
  27. public schedule(callback, interval, key) {
  28. if (this._observers.length <= 0) {
  29. this._lastUpdate = (new Date()).getTime();
  30. director.getScheduler().schedule(this.onUpdateTime.bind(this), this, 0.05, macro.REPEAT_FOREVER, 0, false);
  31. }
  32. let obj = new CountDownObserver(callback, interval, key);
  33. this._observers.push(obj);
  34. }
  35. public onUpdateTime(dt: number) {
  36. let now = (new Date()).getTime();
  37. let deltaTime = (now - this._lastUpdate) / 1000;
  38. this._lastUpdate = now;
  39. for (let i = 0; i < this._observers.length; i++) {
  40. if (this._observers[i] && this._observers[i].update) {
  41. this._observers[i].update(deltaTime);
  42. }
  43. }
  44. return false;
  45. }
  46. /**
  47. * 通过计时器标识查找计时器是否存在
  48. * @param key 计时器标识
  49. * @returns
  50. */
  51. public isScheduled(key) {
  52. for (let i = 0; i < this._observers.length; i++) {
  53. if (this._observers[i]._key == key) {
  54. return true;
  55. }
  56. }
  57. return false;
  58. }
  59. /**
  60. * 移除计时器
  61. * @param key 计时器标识
  62. */
  63. public unschedule(key) {
  64. for (let i = 0; i < this._observers.length; i++) {
  65. if (this._observers[i]._key == key) {
  66. this._observers.splice(i, 1);
  67. break;
  68. }
  69. }
  70. }
  71. /**
  72. * 移除所以计时器
  73. */
  74. public unscheduleAll() {
  75. director.getScheduler().unscheduleAllForTarget(this);
  76. this._observers = [];
  77. }
  78. public changeScheduleKey(oldKey, newKey) {
  79. for (let i = 0; i < this._observers.length; i++) {
  80. if (this._observers[i]._key == oldKey) {
  81. this._observers[i]._key = newKey;
  82. break;
  83. }
  84. }
  85. }
  86. };
  87. class CountDownObserver {
  88. private _interval = 0;
  89. private _elapsed = 0;
  90. private _callback = null;
  91. private _key = null;
  92. public constructor(callback, interval, key) {
  93. this._callback = callback;
  94. this._interval = interval;
  95. this._key = key;
  96. }
  97. public update(dt) {
  98. this._elapsed += dt;
  99. if (this._elapsed > this._interval) {
  100. if (this._callback) {
  101. this._callback(this._elapsed);
  102. }
  103. this._elapsed = 0.0;
  104. }
  105. }
  106. }