mirror of
https://github.com/samber/lo.git
synced 2026-04-22 23:47:11 +08:00
fa095e4b4f
* fix(doc): fix go playground demo URL * fix(doc): add more go playground demo URL
40 lines
906 B
Markdown
40 lines
906 B
Markdown
---
|
|
name: NewDebounce
|
|
slug: newdebounce
|
|
sourceRef: retry.go#L54
|
|
category: core
|
|
subCategory: concurrency
|
|
playUrl: https://go.dev/play/p/_IPY7ROzbMk
|
|
variantHelpers:
|
|
- core#concurrency#newdebounce
|
|
similarHelpers:
|
|
- core#concurrency#newdebounceby
|
|
- core#concurrency#newthrottle
|
|
- core#concurrency#newthrottleby
|
|
- core#concurrency#newtransaction
|
|
- core#concurrency#synchronize
|
|
position: 0
|
|
signatures:
|
|
- "func NewDebounce(duration time.Duration, f ...func()) (func(), func())"
|
|
---
|
|
|
|
Creates a debounced function that delays invoking the callbacks until after the wait duration has elapsed since the last call. Returns the debounced function and a cancel function.
|
|
|
|
```go
|
|
debounce, cancel := lo.NewDebounce(
|
|
100 * time.Millisecond,
|
|
func() {
|
|
println("Called once after debounce!")
|
|
},
|
|
)
|
|
|
|
for i := 0; i < 10; i++ {
|
|
debounce()
|
|
}
|
|
|
|
time.Sleep(200 * time.Millisecond)
|
|
cancel()
|
|
```
|
|
|
|
|