debounce: github.com/bep/debounce Index | Examples | Files

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

Package Files

debounce.go

func New Uses

func New(after time.Duration) func(f func())

New returns a debounced function that takes another functions as its argument. This function will be called when the debounced function stops being called for the given duration. The debounced function can be invoked with different functions, if needed, the last one will win.

Code:

var counter uint64

f := func() {
    atomic.AddUint64(&counter, 1)
}

debounced := debounce.New(100 * time.Millisecond)

for i := 0; i < 3; i++ {
    for j := 0; j < 10; j++ {
        debounced(f)
    }

    time.Sleep(200 * time.Millisecond)
}

c := int(atomic.LoadUint64(&counter))

fmt.Println("Counter is", c)

Output:

Counter is 3

Package debounce imports 2 packages (graph) and is imported by 32 packages. Updated 2020-02-03. Refresh now. Tools for package owners.