Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- func debounce<T>(delay: TimeInterval, function: @escaping (T) -> Void, complete: @escaping () -> Void = { }) -> (T) -> Void {
- let queue = DispatchQueue(label: "Debouncer")
- var current: DispatchWorkItem?
- return { input in
- current?.cancel()
- let new = DispatchWorkItem {
- function(input)
- complete()
- }
- current = new
- queue.asyncAfter(deadline: .now() + delay, execute: new)
- }
- }
Add Comment
Please, Sign In to add comment