Skip to content

Instantly share code, notes, and snippets.

View guillianbalisi's full-sized avatar

Guillian Balisi guillianbalisi

View GitHub Profile
@koingdev
koingdev / Debounce.swift
Created May 1, 2019 14:39
Swift Debouncer
/// Return a new function that will be called only once after `delay` time passed between invocation
func debounce(delay: TimeInterval, queue: DispatchQueue = .main, function: @escaping () -> Void) -> () -> Void {
var currentWorkItem: DispatchWorkItem?
return {
currentWorkItem?.cancel()
currentWorkItem = DispatchWorkItem { function() }
queue.asyncAfter(deadline: .now() + delay, execute: currentWorkItem!)
}
}