Created
September 4, 2014 21:01
-
-
Save ShamylZakariya/54ee03228d955f458389 to your computer and use it in GitHub Desktop.
Simple Swift Debouncer
This file contains hidden or 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( 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() | |
} | |
} | |
} | |
} |
do you have a sample implementation?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Solved it here: http://stackoverflow.com/questions/28411644/search-as-you-type-swift/28412321#28412321