Debounce function for Swift 3
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | |
} | |
} | |
} |
This looks like a complicated delay, not a debounce.
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
Isn't this actually a throttle?