Skip to content

Commit 2363c71

Browse files
authored
Initalize startWait on debounce call (#2)
1 parent 2241f04 commit 2363c71

File tree

1 file changed

+5
-4
lines changed

1 file changed

+5
-4
lines changed

debounce.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,17 +81,18 @@ func (d *debouncer) add(f func()) {
8181
defer d.mu.Unlock()
8282

8383
if d.timer != nil {
84-
d.calls += 1
8584
d.timer.Stop()
8685
} else {
87-
d.calls = 1
86+
d.calls = 0
87+
d.startWait = time.Now()
8888
}
8989

90+
d.calls += 1
91+
9092
// If the function has been called more than the limit, or if the wait time
9193
// has exceeded the limit, execute the function immediately.
9294
if d.callLimitReached() || d.timeLimitReached() {
93-
d.calls = 0
94-
d.startWait = time.Now()
95+
d.timer = nil
9596
f()
9697
} else { // Otherwise, set a timer to call the function after the specified duration.
9798
d.timer = time.AfterFunc(d.after, f)

0 commit comments

Comments
 (0)