diff --git a/assets/core/utils/TimeUtils.ts b/assets/core/utils/TimeUtils.ts index 4ddc0b6..5bfef9a 100644 --- a/assets/core/utils/TimeUtils.ts +++ b/assets/core/utils/TimeUtils.ts @@ -1,6 +1,11 @@ /** 时间工具 */ export class TimeUtil { - /** 间隔天数 */ + /** + * 间隔天数 + * @param time1 开始时间 + * @param time2 结束时间 + * @returns + */ public static daysBetween(time1: number | string | Date, time2: number | string | Date): number { if (time2 == undefined || time2 == null) { time2 = +new Date(); @@ -13,12 +18,10 @@ export class TimeUtil { return dates; } - /** 间隔秒数 */ + /** 间隔秒数,时间顺序无要求,最后会获取绝对值 */ public static secsBetween(time1: number, time2: number) { - if (time2 == undefined || time2 == null) { - time2 = +new Date(); - } let dates = Math.abs((time2 - time1)) / (1000); + dates = Math.floor(dates) + 1; return dates; } diff --git a/assets/libs/gui/label/LabelTime.ts b/assets/libs/gui/label/LabelTime.ts index 8762762..8e8b2d1 100644 --- a/assets/libs/gui/label/LabelTime.ts +++ b/assets/libs/gui/label/LabelTime.ts @@ -1,6 +1,7 @@ import { Label, _decorator } from "cc"; import { oops } from "../../../core/Oops"; import { EventMessage } from "../../../core/common/event/EventMessage"; +import { TimeUtil } from "../../../core/utils/TimeUtils"; const { ccclass, property, menu } = _decorator; @@ -110,17 +111,33 @@ export default class LabelTime extends Label { this.dateDisable = flag; } - /** 设置倒计时时间 */ + /** + * 设置倒计时时间 + * @param second 倒计时时间(单位秒) + */ setTime(second: number) { this.countDown = second; // 倒计时,初始化显示字符串 this.timing_end(); this.timing_start(); + this.format(); + } + + /** + * 设置结束时间戳倒计时 + * @param timeStamp 时间戳 + */ + setTimeStamp(timeStamp: number) { + this.countDown = TimeUtil.secsBetween(oops.timer.getServerTime(), timeStamp); + this.timing_end(); + this.timing_start(); + this.format(); } start() { oops.message.on(EventMessage.GAME_SHOW, this.onGameShow, this); oops.message.on(EventMessage.GAME_HIDE, this.onGameHide, this); this.timing_start(); + this.format(); } onDestroy() { @@ -153,17 +170,16 @@ export default class LabelTime extends Label { private onScheduleComplete() { this.timing_end(); + this.format(); if (this.onComplete) this.onComplete(this.node); } /** 开始计时 */ private timing_start() { this.schedule(this.onScheduleSecond, 1); - this.format(); } private timing_end() { this.unscheduleAllCallbacks(); - this.format(); } }