cocos creator 游戏中常用的2位有效时间整合函数

2019-04-15  本文已影响0人  人气小哥
//时间函数1
function getTime(mss, ptype) {
    let type = ptype || 1

    let dayName
    let hourName
    let minuteName
    let secondName
    if (type == 1) {
        dayName = "天"
        hourName = "时"
        minuteName = "分"
        secondName = "秒"
    } else if (type == 2) {
        dayName = ":"
        hourName = ":"
        minuteName = ":"
        secondName = ""
    } else {
        return "type 不正确 请检查参数"
    }

    var days = parseInt(mss / (1000 * 60 * 60 * 24));
    var hours = parseInt((mss % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
    var minutes = parseInt((mss % (1000 * 60 * 60)) / (1000 * 60));
    var seconds = (mss % (1000 * 60)) / 1000;

    // cc.log(days)
    // cc.log(hours)
    // cc.log(minutes)
    // cc.log(seconds)

    if (days > 0) {
        //cc.log("X天X小时")
        return days + dayName + hours + hourName
    } else if (hours > 0) {
        //cc.log("X时X分")

        return hours + hourName + minutes + minuteName
    } else if (minutes > 0) {
        //cc.log("X分X秒")

        return minutes + minuteName + seconds + secondName
    } else if (seconds > 0) {
        //cc.log("X秒")

        return seconds + secondName
    }

    cc.log("时间已经为负数 显示 0秒 或者返回 null")
    return "0" + secondName//days + dayName + hours + hourName + minutes + minuteName + seconds + secondName;
}
window.g_getTime = getTime
  //倒计时
        let time = 1000 * 60 * 60//Date.now();

        var self = this
        this.schedule(function() {
            // 这里的 this 指向 component
            //

            time = time - 1000
            this.timeLabel.string = g_getTime(time, 2)
            
        }, 1);
上一篇 下一篇

猜你喜欢

热点阅读