debounce

package
v0.22.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 30, 2025 License: GPL-3.0 Imports: 2 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func New

func New(after time.Duration) func(f func())
Example (V1)
var counter uint64

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

debounced := 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
Example (V2)
var counter1 uint64
var counter2 uint64

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

f2 := func() {
	atomic.AddUint64(&counter2, 1)
}

debounced := New(100 * time.Millisecond)

debounced(f) // will be reset and dropped!!!
for i := 0; i < 10; i++ {
	debounced(f2)
}

time.Sleep(time.Millisecond * 500)

c1 := int(atomic.LoadUint64(&counter1))
c2 := int(atomic.LoadUint64(&counter2))

fmt.Println("Counter1 is", c1)
fmt.Println("Counter2 is", c2)
Output:
Counter1 is 0
Counter2 is 1

Types

type Debounce

type Debounce struct {
	sync.Mutex
	// contains filtered or unexported fields
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL