package debounce
import "github.com/bep/debounce"
Package debounce provides a debouncer func. The most typical use case would be the user typing a text into a form; the UI needs an update, but let's wait for a break.
Index ¶
Examples ¶
Functions ¶
func New ¶
New returns a debounced function and two channels:
1. A quit channel that can be closed to signal a stop
2. A done channel that signals when the debouncer is completed
of the goroutine.
The function will, as long as it continues to be invoked, not be triggered.
The function will be called after it stops being called for the given duration.
The created debounced function can be invoked with different functions, if needed,
the last one will win.
Also note that a stop signal means a full stop of the debouncer; there is no
concept of flushing future invocations.
Code:play
Output:Example¶
package main
import (
"fmt"
"sync/atomic"
"time"
"github.com/bep/debounce"
)
func main() {
var counter uint64
f := func() {
atomic.AddUint64(&counter, 1)
}
debounced, finish, done := debounce.New(100 * time.Millisecond)
for i := 0; i < 3; i++ {
for j := 0; j < 10; j++ {
debounced(f)
}
time.Sleep(200 * time.Millisecond)
}
close(finish)
<-done
c := int(atomic.LoadUint64(&counter))
fmt.Println("Counter is", c)
}
Counter is 3
Source Files ¶
- Version
- v1.1.0
- Published
- Apr 8, 2018
- Platform
- js/wasm
- Imports
- 1 packages
- Last checked
- now –
Tools for package owners.