Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions debounce.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,15 @@ func New(after time.Duration, options ...Option) func(f func()) {
}
}

// NewFunc return debounce function, that calls f every time.
func NewFunc(f func(), after time.Duration, options ...Option) func() {
debounce := New(after, options...)

return func() {
debounce(f)
}
}

type debouncer struct {
mu sync.Mutex
after time.Duration
Expand Down
18 changes: 18 additions & 0 deletions example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,21 @@ func ExampleNew() {
time.Sleep(1 * time.Second)
// Output: This will be executed instead
}

func ExampleNewFunc() {
a := 0
// Create a debounced function with 500ms delay
debouncedFunc := debounce.NewFunc(func() {
a += 1
fmt.Println(a)
}, 500*time.Millisecond)

// This will only execute once, after 500ms
debouncedFunc()

debouncedFunc()

// Wait for execution
time.Sleep(1 * time.Second)
// Output: 1
}
Loading