倒计时天时分秒函数(cocos-js)

倒计时天时分秒函数(cocos-js)

  • 第一次写的方法,和优化后的方法
    • 第一次
    • 优化后
    • 在onload()中创建定时器
    • 感谢

第一次写的方法,和优化后的方法

第一次

doSomething1(second_time) {
        if (parseInt(second_time) < 60) {
            var time = "倒计时:" + "00" +
                "天" + "00" +
                "时" + "00" +
                "分" + second_time +
                "秒";
        }
        if (parseInt(second_time) > 60) {
            var second = parseInt(second_time) % 60;
            //秒数补零
            var seco = second > 9 ? second : "0" + second;
            var min = parseInt(second_time / 60);
            var time = "倒计时:" + "00" +
                "天" + "00" +
                "时" + min1 +
                "分" + seco +
                "秒";
            if (min > 60) {
                min = parseInt(second_time / 60) % 60;
                //分数补零
                var min1 = min > 9 ? min : "0" + min;
                var hour = parseInt(parseInt(second_time / 60) / 60);
                var hour1 = hour > 9 ? hour : "0" + hour;
                var time = "倒计时:" + "00" +
                    "天" + hour1 +
                    "时" + min1 +
                    "分" + seco +
                    "秒";
                if (hour > 24) {
                    hour = parseInt(parseInt(second_time / 60) / 60) % 24;
                    //时数补零
                    var hour1 = hour > 9 ? hour : "0" + hour;
                    var day = parseInt(parseInt(parseInt(second_time / 60) / 60) / 24);
                    //天数补零
                    var day1 = day > 9 ? day : "0" + day;
                    var time = "倒计时:" + day1 +
                        "天" + hour1 +
                        "时" + min1 +
                        "分" + seco +
                        "秒";
                }
            }
        }
        this.richtxt.getComponent(cc.RichText).string = time;
        // console.log("times: " + time);
    },

优化后

 doSomething: function(sec) {
        var minute = parseInt(sec / 60);
        var second = parseInt(sec % 60);
        var hour = parseInt(minute / 60);
        var hour1 = hour > 60 ? hour % 24 : hour;
        var day = parseInt(hour / 24);
        minute = minute % 60;
        var timeStr = "";
        var timeStr = hour <= 0 ? timeStr = "倒计时:" +
            "00" + "天" + "00" + "时" +
            parseInt(minute / 10) + "" + minute % 10 + "分" +
            parseInt(second / 10) + "" + second % 10 + "秒" : timeStr = "倒计时:" +
            parseInt(day / 10) + "" + day % 10 + "天" +
            parseInt(hour1 / 10) + "" + hour1 % 10 + "时" +
            parseInt(minute / 10) + "" + minute % 10 + "分" +
            parseInt(second / 10) + "" + second % 10 + "秒";
        this.richtxt.getComponent(cc.RichText).string = timeStr;
    },

在onload()中创建定时器

onLoad() {
        this.Time = 5000;
        //创建一个定时器
        this.schedule(function() {
            this.doSomething(this.Time--);
        }, 1);
    },

感谢

这里是使用的富文本的方式写的。

如图

你可能感兴趣的:(cocos2d-x,数据结构,cocos2d-js)