Skip to content

Instantly share code, notes, and snippets.

@AndreyPanov
Created August 10, 2017 16:59
Show Gist options
  • Save AndreyPanov/f3c9ccdf1afc99b07d919c3f119b4d9b to your computer and use it in GitHub Desktop.
Save AndreyPanov/f3c9ccdf1afc99b07d919c3f119b4d9b to your computer and use it in GitHub Desktop.
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
Copy link

Isn't this actually a throttle?

@benaubin
Copy link

benaubin commented Jan 3, 2018

This looks like a complicated delay, not a debounce.

@hauptanja
Copy link

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