idanizi / gobounce

Debounce & Throttle inspired by Lodash's debounce and throttle

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

gophercises_jumping

gobounce

Debounce & Throttle inspired by Lodash's debounce and throttle

Usage

Debounce

foo := func() {
  // do somthing anoying
}

var options *DebounceOptions // if nil, will have default options

debounced, cancel := gobounce.Debounce(foo, 2*time.Second, options)
defer cancel() // should be called to dispose timers and flush

debounced()
debounced()
debounced()
debounced()
debounced()
...
// after 2 seconds...
...
// foo called only once!

Throttle

foo := func() {
  // do somthing exausting
}

var options *ThrottleOptions // if nil, will have default options

throttled, cancel := gobounce.Throttle(foo, 2*time.Second, options)
defer cancel() // should be called to dispose timers and flush

// ------------- 00:00:00
throttled() // - 00:01:00
throttled() // - 00:02:00 - called!
throttled() // - 00:03:00
throttled() // - 00:04:00 - called!
throttled() // - 00:05:00
throttled() // - 00:06:00 - called!
// ------------- 00:07:00

Author

Idan Izicovich

About

Debounce & Throttle inspired by Lodash's debounce and throttle

License:MIT License


Languages

Language:Go 100.0%