Skip to content

Instantly share code, notes, and snippets.

@ShamylZakariya
Created September 4, 2014 21:01
Show Gist options
  • Star 21 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ShamylZakariya/54ee03228d955f458389 to your computer and use it in GitHub Desktop.
Save ShamylZakariya/54ee03228d955f458389 to your computer and use it in GitHub Desktop.
Simple Swift Debouncer
func debounce( delay:NSTimeInterval, #queue:dispatch_queue_t, action: (()->()) ) -> ()->() {
var lastFireTime:dispatch_time_t = 0
let dispatchDelay = Int64(delay * Double(NSEC_PER_SEC))
return {
lastFireTime = dispatch_time(DISPATCH_TIME_NOW,0)
dispatch_after(
dispatch_time(
DISPATCH_TIME_NOW,
dispatchDelay
),
queue) {
let now = dispatch_time(DISPATCH_TIME_NOW,0)
let when = dispatch_time(lastFireTime, dispatchDelay)
if now >= when {
action()
}
}
}
}
@anoop4real
Copy link

do you have a sample implementation?

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