// 延时计时器
|
function Timeout() {
|
this.timer = null;
|
this.time = '';
|
this.callback = '';
|
this.isStop = false;
|
}
|
// 开启计时器并添加
|
Timeout.prototype.start = function(callback, time, exe) {
|
// 查看isStop
|
if(exe == 'exe' && this.isStop) {
|
return;
|
}
|
// 先关闭计时器
|
this.stop();
|
this.isStop = false;
|
// 配置执行函数
|
if(typeof callback == 'function' && typeof time == 'number') {
|
this.callback = callback;
|
this.time = time;
|
if(exe != 'exe') {
|
callback();
|
}else {
|
this.isStop = false;
|
}
|
|
this.timer = setTimeout(callback, time);
|
}else {
|
console.warn('未完整配置参数!');
|
}
|
};
|
// 开启计时器
|
Timeout.prototype.open = function() {
|
var callback = this.callback;
|
var time = this.time;
|
this.start(callback, time, 'exe');
|
};
|
|
// 关闭计时器
|
Timeout.prototype.stop = function() {
|
this.isStop = true;
|
clearTimeout(this.timer);
|
};
|
|
export default Timeout;
|