Files
photoview/api/utils/Throttle.go
viktorstrate 7b3f016a0f Add replace debounce with throttle
Update dependencies
2020-03-12 13:26:11 +01:00

23 lines
376 B
Go

package utils
import "time"
type Throttle struct {
interval time.Duration
lastAction time.Time
}
func NewThrottle(interval time.Duration) Throttle {
return Throttle{
interval: interval,
lastAction: time.Unix(0, 0),
}
}
func (t *Throttle) Trigger(action func()) {
if time.Now().After(t.lastAction.Add(t.interval)) {
t.lastAction = time.Now()
action()
}
}