JS实现pool轮询函数

function myPool(cs, cb, interval){
	if(cs()){
		return cb();
	} else{
		setTimeout(() => { myPool(cs, cb); }, interval);
	}
}

function checkStatus() {
	var i = 0;
	var arr = [0,0,0,1,1];
	return () => { console.log('POOL ' + i + ' Times!'); return Boolean(arr[i++]); }
}

function callBack(){
	console.log('hello!');
}

myPool(checkStatus(), callBack, 1000);

运行结果:

POOL 1 Times!
POOL 2 Times!
POOL 3 Times!
hello!

猜你喜欢

转载自blog.csdn.net/lianfengzhidie/article/details/80864688