debounce

package module
v1.2.1 Latest Latest
Warning

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

Go to latest
Published: May 15, 2022 License: MIT Imports: 2 Imported by: 292 modules, 277 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

Go Debounce

Tests on Linux, MacOS and Windows GoDoc Go Report Card codecov Release

Example

func ExampleNew() {
	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
}

Documentation

Overview

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

Constants

This section is empty.

Variables

This section is empty.

Functions

func New

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.

Example
package main

import (
	"fmt"
	"sync/atomic"
	"time"

	"github.com/bep/debounce"
)

func main() {
	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

Types

This section is empty.

Jump to

Keyboard shortcuts

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