timeout
# Node.js
setTimeout(callback, 1e3);
function callback() {
console.log("called");
}
1
2
3
4
5
2
3
4
5
Output
called
1
# Go
package main
import (
"fmt"
"sync"
"time"
)
var wg sync.WaitGroup
func callback() {
defer wg.Done()
fmt.Println("called")
}
func main() {
wg.Add(1)
time.AfterFunc(1*time.Second, callback)
wg.Wait()
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
Output
called
1
编辑 (opens new window)
上次更新: 2022/09/30, 11:34:22