定时器和promise_前端开发基础Promise 与定时器

"use strict"

function Wait(interval, beforeTime, afterTime, limit){

this.every(interval, limit);

this.before(beforeTime);

this.after(afterTime | 0);

}

Wait.prototype = {

before: function(time){

this.startTime = Date.now();

this.expires = this.startTime + time;

return this;

},

after: function(time){

this.afterTime = time;

return this;

},

every: function(interval, limit){

this.interval = interval;

if(limit != null) this.limit(limit);

return this;

},

limit: function(limit){

limit = limit > 0 ? limit : Infinity;

this.limit = limit;

return this;

},

check: function(cond){

cond = cond || function(){};

return this.before(0).until(cond);

},

till: function(cond){

var self = this;

return this.until(function(){

var res;

try{

res = cond();

return res === true;

}catch(ex){

self.limit = 0; //force error

throw ex;

}

});

},

until: function(cond){

var interval = this.interval,

afterTime = this.afterTime,

self = this;

var timer, called = 0;

return new Promise(function(resolve, reject){

function f(){

var err, res;

called++;

try{

res = cond();

}catch(ex){

err = ex;

}finally{

if(Date.now() >= self.expires || called >= self.limit){

clearInterval(timer);

if(err !== undefined || res === false){

reject(err || new Error("check failed"));

}else{

resolve(res);

}

return true;

}else if(err === undefined & res !== false){

clearInterval(timer);

resolve(res);

return true;

}

return false;

}

}

setTimeout(function(){

if(!f()){

timer = setInterval(f, interval);

}

}, afterTime);

});

}

};

module.exports = {

every: function(interval, limit){

return new Wait(interval, Infinity, 0, limit);

},

limit: function(limit){

return new Wait(100, Infinity, 0, limit);

},

before: function(time, limit){

return new Wait(100, time, 0, limit);

},

after: function(time){

return new Wait(100, Infinity, time);

},

sleep: function(time){

return new Wait(100, Infinity, time).check();

},

until: function(cond){

return (new Wait(100, Infinity)).until(cond);

},

till: function(cond){

return (new Wait(100, Infinity)).till(cond);

},

check: function(cond){

return (new Wait(100, 0)).until(cond);

}

};

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值