Instantly share code, notes, and snippets.

Embed
What would you like to do?
Debounce function for Swift 3
func debounce(_ action: @escaping () -> Void) {
let lastFireTime = DispatchTime.now()
let dispatchDelay = TimeInterval(0.3)
DispatchQueue.main.asyncAfter(deadline: .now() + dispatchDelay) {
let now = DispatchTime.now()
let when = lastFireTime + dispatchDelay
if now >= when {
action()
}
}
}
@fjcaetano

This comment has been minimized.

fjcaetano commented Dec 20, 2017

Isn't this actually a throttle?

@benaubin

This comment has been minimized.

benaubin commented Jan 3, 2018

This looks like a complicated delay, not a debounce.

@hauptanja

This comment has been minimized.

hauptanja commented Sep 24, 2018

This looks like a complicated delay, not a debounce.

Why complicated? Looks basic to me

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment