-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtime.js
More file actions
42 lines (35 loc) · 923 Bytes
/
Copy pathtime.js
File metadata and controls
42 lines (35 loc) · 923 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
const 同步等待 = (秒数=1000,开启日志=false) => {
开启日志&&console.time("等待计时");
开启日志&&console.log('开始等待');
const startTime = new Date().getTime()
while (true){
const time = new Date().getTime()
if(time>=startTime+(秒数)){
开启日志&&console.timeEnd("等待计时");
break
}
}
}
const 异步等待 = (回调函数=()=>{},秒数=1000) => {
const timer = setTimeout(()=>{
回调函数()
clearTimeout(timer)
}, 秒数);
}
const 循环定时器 = (回调函数=(n=0)=>{},秒数=1000) => {
let n = 0
const timer = setInterval(() => {
n++
回调函数(n)
}, 秒数);
const 清理函数 = ()=>clearInterval(timer)
return {清理函数}
}
const 时间 = {
同步等待,
异步等待,
循环定时器
}
module.exports ={
时间,
}