js 实现 类似java的sleep和wait
sleepwait
sleep
var mySleep
= async function (time
, func
) {
return new Promise(function (resolve
, reject
) {
setTimeout(() => {
if (func
)
resolve(func())
}, time
);
})
}
let res
= await mySleep(1000,()=>{})
wait
var myWait
= async function (intervalTime
, mxWaitTime
, fun
, timeOutFun
) {
let res
= func()
let time1
= 0
while (res
!== true || (mxWaitTime
!==-1&&time1
< mxWaitTime
)) {
res
= await mySleep(intervalTime
, func())
time1
+= intervalTime
}
if ((mxWaitTime
!==-1&&time1
>= mxWaitTime
) && timeOutFun
)
timeOutFun()
}
var aaa
=0;
setTimeout(()=>{aaa
=3},5000);
let res
= await myWait(1000,10000,()=>{return aaa
==3},()=>{})
转载请注明原文地址: https://lol.8miu.com/read-17037.html