CocosCreator 源码​ profiler/counter.js详解

let Counter = cc.Class({
    name: 'cc.Counter',
    ctor(id, opts, now) {
        /* 初始化所有参数,id是key,opts是object,now是浏览器打开到当前时间,其他参数默认为0 ,_accumStart为now*/
        this._id = id;
        this._opts = opts || {};

        this._value = 0;
        this._total = 0;
        this._averageValue = 0;
        this._accumValue = 0;
        this._accumSamples = 0;
        this._accumStart = now;
    },

    properties: {
        value: {
            get() {
                return this._value;
            },
            set(v) {
                this._value = v;
            }
        }
    },

    /* 查了下调用 _average的地方,当第二个参数为空的时候,一定不会进入重置逻辑,只会样本count++*/
    _average(v, now) {
        /* 参数v是时间差 */
        if (this._opts.average) {/* 初始化数据源的时候默认为500,有值 */
            this._accumValue += v;/* 初始化为0,一直增加时间差值 */
            ++this._accumSamples;/* 样本数量++ */

            let t = now;
            if (t - this._accumStart >= this._opts.average) {
                this._averageValue = this._accumValue / this._accumSamples;
                this._accumValue = 0;
                this._accumStart = t;
                this._accumSamples = 0;
            }
        }
    },

    sample(now) {
        this._average(this._value, now);//计算平均值
    },

    human() {
        /* 配置假如存在average,logic ,render都是默认500; */
        let v = this._opts.average ? this._averageValue : this._value;
        /* 返回提供的数值表达式,四舍五入到最接近的整数。 */
        return Math.round(v * 100) / 100;

        setInterval(() => {
            console.log(this._averageValue);
        }, 100)
    },
    /* 没有地方明文调用 是否超过了 初始化设置的值 或者低于 初始化的值*/
    alarm() {
        return (
            (this._opts.below && this._value < this._opts.below) ||
            (this._opts.over && this._value > this._opts.over)
        );
    }
})

module.exports = Counter;

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值