javascript实现动态定制定时器,具体代码如下:
function timeout (){
if(typeof timeout._instance === 'object'){
return timeout._instance;
}
function move(time, fn, config) {
function loop() {
fn(config);
time.id = setTimeout(function () {
if (time.num < time.count) {
loop();
time.num++;
} else if (time.num == time.count) {
clearTimeout(self.id);
time.id = null;
time.num = 0;
}
}, time.delay);
}
loop();
}
function stop(time) {
clearTimeout(time.id);
time.id = null;
time.num = 0;
}
timeout._instance = {
on:move,
off:stop
};
return timeout._instance;
};
更多信息请查看IT技术专栏