Laya时间相关信息的二次封装(每日任务,倒计时)

直接上代码。。。
时间类

import DataStorage from "./DataStorage";

export default class MyTime {

    /**
     * 存储当前时间
     * @param type 标签 
     */
    public static SaveTime(type: string): number {
        var d: Date = new Date();
        var nowTime = d.getTime();
        DataStorage.Save(type + "MyTime", nowTime + "");
        return nowTime;
    }
    /**
     * 适用每日任务的信息存储
     * @param type 
     * @param data 
     */
    public static SavaDay(type: string, data): void {
        var d: Date = new Date();
        var month = d.getDate();
        var day = d.getMonth()
        // month + ":" + day + "mission" + tpye
        DataStorage.Save(month + ":" + day + "mission" + type, data + "");
    }
    /**
     * 适用每日任务的信息获取
     * @param type 
     * @param data 
     */
    public static GetDay(type: string): string {
        var d: Date = new Date();
        var month = d.getDate();
        var day = d.getMonth()
        return DataStorage.Get(month + ":" + day + "mission" + type);
    }
    /**
     * 获取存储的时间
     * @param type 标签 
     */
    public static TimeIsExist(type: string): boolean {
        return DataStorage.Get(type + "MyTime");
    }
    /**
    * 返回的结果是距离上次存储时间过了多少秒
    * @param type 标签 
    */
    public static GetTimeOfSecond(type: string): number {
        var lastTime = Number(DataStorage.Get(type + "MyTime"));
        var d: Date = new Date();
        var nowTime = d.getTime();
        var lerptime = nowTime - lastTime;
        return Math.floor(lerptime / 1000);

    }
    /**
    * 返回的结果是距离上次存储时间过了多少分钟
    * @param type 标签 
    */
    public static GetTimeOfMinute(type: string): number {
        var lastTime = Number(DataStorage.Get(type + "MyTime"));
        var d: Date = new Date();
        var nowTime = d.getTime();
        var lerptime = Math.floor((nowTime - lastTime) / 1000);
        return Math.floor(lerptime / 60);
    }
    /**
    * 返回的结果是距离上次存储时间过了多少小时
    * @param type 标签 
    */
    public static GetTimeOfHour(type: string): number {
        var lastTime = Number(DataStorage.Get(type + "MyTime"));
        var d: Date = new Date();
        var nowTime = d.getTime();
        var lerptime = Math.floor(Math.floor((nowTime - lastTime) / 1000) / 60);
        return Math.floor(lerptime / 60);
    }
    /**
    * 返回的结果是距离上次存储时间过了多少时:分:秒
    * @param type 标签 
    */
    public static GetTime(type: string): string {
        var s = this.GetTimeOfSecond(type);
        var m = Math.floor(s / 60);
        var h = Math.floor(m / 60);
        s = s % 60;
        m = m % 60;
        h = h % 24;
        var ss, mm, hh;
        if (s < 10) ss = "0" + s;
        else ss = s;
        if (m < 10) mm = "0" + m;
        else mm = m;
        if (h < 10) hh = "0" + h;
        else hh = h;
        return hh + ":" + mm + ":" + ss;
    }
    /**
    * 倒计时 时:分:秒
    * @param type 标签 
    * @param allTime 总时长(秒)
    */
    public static CountDown(type: string, allTime: number): string {
        var s = this.GetTimeOfSecond(type);
        s = allTime - s;
        if (s <= 0) s = 0;
        var m = Math.floor(s / 60);
        var h = Math.floor(m / 60);
        s = s % 60;
        m = m % 60;
        h = h % 24;
        var ss, mm, hh;
        if (s < 10) ss = "0" + s;
        else ss = s;
        if (m < 10) mm = "0" + m;
        else mm = m;
        if (h < 10) hh = "0" + h;
        else hh = h;
        return hh + ":" + mm + ":" + ss;
    }
    /**
    * 倒计时 秒
    * @param type 标签 
    * @param allTime 总时长(秒)
    */
    public static CountDownOfSecond(type: string, allTime: number): number {
        var s = this.GetTimeOfSecond(type);
        s = allTime - s;
        if (s <= 0) s = 0;
        return s;
    }
    /**
   * 倒计时 秒
   * @param type 标签 
   * @param allTime 总时长(秒)
   */
    public static CountDownIsReady(type: string, allTime: number): boolean {
        var s = this.GetTimeOfSecond(type);
        s = allTime - s;
        return s <= 0;
    }
}

上面用到了另一个存储类代码如下


/**数据存储类 */
export default class DataStorage {

    /**保存数据 */
    public static Save(key: string, value: string): void {
        Laya.LocalStorage.setItem(key + "MyDataStorage", value);
    }
    
    /**获取数据 */
    public static Get(key: string): any {
        return Laya.LocalStorage.getItem(key + "MyDataStorage");
    }
}

完成。。。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值