A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

使用新的方式来实现倒计时的显示

在进行倒计的时候只需要记录几个变量
1,总的倒计时时间,
2,倒计时开始的时候的时间戳
3,是否进行新的一轮倒计时的标志

实现方法

新建一个脚本管理数据脚本如下

var dataOperate = require("dataOperateFunc");

var StoreData = {
    // 数据发在内部,提供接口访问
    _totalCountdownNum: 7200,   //倒计时时间
    _isUse: false,   //免费刷新是否使用
    _timestampOfDeparture: 0,   //倒计时的时间戳

    InitData: function () {
        var tempData = dataOperate.getBlobMapByKey("store_data");
        var date = new Date();
        console.log("#### store data", tempData);
        if (!!tempData) {;
            this._isUse = tempData.is_use;
            this._timestampOfDeparture = tempData.timestamp_of_departure;

            if (date.getDate() != tempData.last_date) {
                this._refreshNum = 1;
            } else {
                this._refreshNum = tempData.refresh_num;
            }
        } else {
            this._timestampOfDeparture = date.getTime();
            // console.log("!!! not have guide info use default", this.isGuide, this.stageName, this.stageStep);   
        }
    },

    SetData: function () {
        var date = new Date();
        var currentDate = date.getDate();

        var tempData = {
            last_date: currentDate,
            refresh_num: this._refreshNum,
            timestamp_of_departure: this._timestampOfDeparture,
            is_use: this._isUse,
            buy_num: this._buyNum,
        }

        dataOperate.updateBlobMapKeyValue({
            store_data: tempData
        });
    },

    GetCountdownInfo: function () {
        let data = {
            isUse: this._isUse,
            timestamp: this._timestampOfDeparture,
            totalCountdownTime: this._totalCountdownNum,
        }
        return data;
    },

    /**
     * 设置倒计时的相关信息
     * @param {boolean} useState 免费刷新的使用状态
     * @param {number} timestamp 使用时的时间戳
     */
    SetCountdownInfo: function (useState, timestamp) {
        this._isUse = useState;
        if (!!timestamp) {
            this._timestampOfDeparture = timestamp;
        }
        this.SetData();
    },

}

module.exports = StoreData;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
在建立一个脚本进行倒计时的显示代码如下

var storeData = require("StoreData"); //引入倒计时的数据

cc.Class({
    extends: cc.Component,
        infoLabel: cc.Label,
    },

    onLoad() {
        this._countdownSecond = 0;        //   **用于在当前界面的倒计时计算**  
        this.intervalTime = 1;        //  **用于每秒进行一下计算**   

    },

    onEnable() {
        this.InitCountdown();
    },

    start() {},

    update(dt) {
        if (this.intervalTime >= 0) {
            this.intervalTime -= dt;
        } else {
            this.intervalTime = 1;
            if (this._countdownSecond >= -1) {
                this.UpdateCountdown();
            }
        }
    },

    InitCountdown: function () {
        var countdownInfo = storeData.GetCountdownInfo();
        // console.log("#### 商店倒计时信息", countdownInfo);
        var date = new Date();
        if (countdownInfo.isUse) {
            this._countdownSecond = countdownInfo.totalCountdownTime - Math.floor((date.getTime() - countdownInfo.timestamp) / 1000);
            //  **当前剩余的倒计时数用倒计总时间减去已经过去的时间**  
        } else {
            this._countdownSecond = 0;
        }

        this.UpdateCountdown();
    },

    UpdateCountdown: function () {
        var baseSecond = this._countdownSecond;
        var countdownInfo = storeData.GetCountdownInfo();
        if (baseSecond >= 1) {
            baseSecond -= 1;
            this._countdownSecond -= 1;
            var hour = Math.floor(baseSecond / 3600);
            var residue = baseSecond - hour * 3600;
            var minute = Math.floor(residue / 60);
            residue = Math.floor(residue - minute * 60);
            if (hour < 10) {
                hour = "0" + hour;
            }
            if (minute < 10) {
                minute = "0" + minute;
            }
            if (residue < 10) {
                residue = "0" + residue;
            }
            // this.infoLabel.getComponent(cc.Label).string = hour + " : " + minute + " : " + residue + " 后免费" + this.PriceCalculation();
        } else {
            if (countdownInfo.isUse) {
                let date = new Date();
                storeData.SetCountdownInfo(false, date.getTime());
                // this.infoLabel.getComponent(cc.Label).string = "" + this.PriceCalculation();
            }
        }
    },
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
这个优化主要是减少了一些用于控制的变量,使得在初始化倒计时变得简单,同时也不用在每次关闭窗口时就要记录一下时间戳了。
---------------------
【转载,仅作分享,侵删】
作者:liuzhimu2018
原文:https://blog.csdn.net/liuzhimu2018/article/details/85990467


1 个回复

倒序浏览
奈斯
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马