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;
06-21
335
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
09-03
1590
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
“相关推荐”对你有帮助么?
-
非常没帮助
-
没帮助
-
一般
-
有帮助
-
非常有帮助
提交