代码拉取完成,页面将自动刷新
/***
* 此js文件中的构造函数、方法用于修复异常api:::
*
* SetInterval Function构造函数 修复setInterval每次定时执行的偏差,适用于倒计时场景
* 用法:
* var test = new SetInterval(callback, 1000); //开始定义倒计时
* test.$clear(); // 移除
*/
//修复setInterval每次定时执行的偏差,适用于倒计时场景
/**
* callback: 每次回调执行函数
* time: 每隔多少时间回调一次
*/
function SetInterval(callback, time) {
this.start = new Date().getTime();
this.end;
this.offset;
this.loopNum = 0;
this.timeoutTag = null;
this.time = time;
this.callback = callback;
this.$set(time);
}
SetInterval.prototype.$set = function (_time) {
var that = this;
if (this.timeoutTag === undefined) return;
clearTimeout(this.timeoutTag);
this.timeoutTag = null;
that.timeoutTag = setTimeout(function () {
that.loopNum++;
that.end = new Date().getTime();
that.callback();
that.offset = that.end - that.start - that.time * that.loopNum;
//console.log("333,,,偏移了" + that.offset);
if (that.offset > 0) {
that.$set(that.time - that.offset);
} else if (that.offset < 0) {
that.$set(that.time + that.offset);
} else {
that.$set(that.time);
}
}, _time);
};
SetInterval.prototype.$clear = function () {
clearTimeout(this.timeoutTag);
//便于拦截区分,此处必须设置为undefined
this.timeoutTag = undefined;
};
export {
SetInterval
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。