Created
August 10, 2017 16:59
-
-
Save AndreyPanov/f3c9ccdf1afc99b07d919c3f119b4d9b to your computer and use it in GitHub Desktop.
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.
Why complicated? Looks basic to me
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This looks like a complicated delay, not a debounce.