c925ae390c859b5b44e63eb849b7edba91fff594.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. System.register(["cc"], function (_export, _context) {
  2. "use strict";
  3. var _cclegacy, Timer, _crd;
  4. _export("Timer", void 0);
  5. return {
  6. setters: [function (_cc) {
  7. _cclegacy = _cc.cclegacy;
  8. }],
  9. execute: function () {
  10. _crd = true;
  11. _cclegacy._RF.push({}, "6021fct1uhJsImEuhdFWC0f", "Timer", undefined);
  12. /*
  13. * @Author: dgflash
  14. * @Date: 2023-01-19 11:09:38
  15. * @LastEditors: dgflash
  16. * @LastEditTime: 2023-01-19 14:28:05
  17. */
  18. /**
  19. * 定时触发组件
  20. * @help https://gitee.com/dgflash/oops-framework/wikis/pages?sort_id=12037964&doc_id=2873565
  21. * @example
  22. export class Test extends Component {
  23. // 创建一个定时跳动组件
  24. private timer: Timer = new Timer(1);
  25. update(dt: number) {
  26. if (this.timer.update(this.dt)) {
  27. console.log(每一秒触发一次);
  28. }
  29. }
  30. }
  31. */
  32. _export("Timer", Timer = class Timer {
  33. get elapsedTime() {
  34. return this._elapsedTime;
  35. }
  36. /** 触发间隔时间(秒) */
  37. get step() {
  38. return this._step;
  39. }
  40. set step(step) {
  41. this._step = step; // 每次修改时间
  42. this._elapsedTime = 0; // 逝去时间
  43. }
  44. get progress() {
  45. return this._elapsedTime / this._step;
  46. }
  47. /**
  48. * 定时触发组件
  49. * @param step 触发间隔时间(秒)
  50. */
  51. constructor(step = 0) {
  52. this.callback = null;
  53. this._elapsedTime = 0;
  54. this._step = -1;
  55. this.step = step;
  56. }
  57. update(dt) {
  58. if (this.step <= 0) return false;
  59. this._elapsedTime += dt;
  60. if (this._elapsedTime >= this._step) {
  61. var _this$callback;
  62. this._elapsedTime -= this._step;
  63. (_this$callback = this.callback) == null || _this$callback.call(this);
  64. return true;
  65. }
  66. return false;
  67. }
  68. reset() {
  69. this._elapsedTime = 0;
  70. }
  71. stop() {
  72. this._elapsedTime = 0;
  73. this.step = -1;
  74. }
  75. });
  76. _cclegacy._RF.pop();
  77. _crd = false;
  78. }
  79. };
  80. });
  81. //# sourceMappingURL=c925ae390c859b5b44e63eb849b7edba91fff594.js.map