Guest User

Untitled

a guest
Jan 17th, 2019
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.45 KB | None | 0 0
  1. func debounce<T>(delay: TimeInterval, function: @escaping (T) -> Void, complete: @escaping () -> Void = { }) -> (T) -> Void {
  2. let queue = DispatchQueue(label: "Debouncer")
  3. var current: DispatchWorkItem?
  4.  
  5. return { input in
  6. current?.cancel()
  7. let new = DispatchWorkItem {
  8. function(input)
  9. complete()
  10. }
  11. current = new
  12. queue.asyncAfter(deadline: .now() + delay, execute: new)
  13. }
  14. }
Add Comment
Please, Sign In to add comment