42 lines
951 B
JavaScript
42 lines
951 B
JavaScript
/*-----------------------------------------------------+
|
|
* 定时器相关处理
|
|
* @author whjing2012@163.com
|
|
+-----------------------------------------------------*/
|
|
|
|
var Timer = {
|
|
|
|
idx: 1,
|
|
timer: {},
|
|
|
|
getInstance: function() {
|
|
return this;
|
|
},
|
|
|
|
set: function( func, time, num, id ) {
|
|
time = time || 1000;
|
|
num = num || 1;
|
|
if( !id ) {
|
|
id = this.idx++;
|
|
}
|
|
this.del( id );
|
|
this.timer[ id ] = window.setTimeout( ( function() {
|
|
if( num == 1 ) {
|
|
this.del( id );
|
|
} else {
|
|
this.set( func, time, num - 1, id );
|
|
}
|
|
func();
|
|
} ).bind( this ), time );
|
|
return id;
|
|
},
|
|
|
|
del: function( id ) {
|
|
if( this.timer.hasOwnProperty( id ) ) {
|
|
window.clearTimeout( this.timer[ id ] );
|
|
delete this.timer[ id ];
|
|
}
|
|
}
|
|
};
|
|
|
|
module.exports = Timer;
|