Skip to content

Instantly share code, notes, and snippets.

Last active September 28, 2020 14:52
What would you like to do?
import Foundation
class Debouncer {
Create a new Debouncer instance with the provided time interval.
- parameter timeInterval: The time interval of the debounce window.
init(timeInterval: TimeInterval) {
self.timeInterval = timeInterval
typealias Handler = () -> Void
/// Closure to be debounced.
/// Perform the work you would like to be debounced in this handler.
var handler: Handler?
/// Time interval of the debounce window.
private let timeInterval: TimeInterval
private var timer: Timer?
/// Indicate that the handler should be invoked.
/// Begins the debounce window with the duration of the time interval parameter.
func renewInterval() {
// Invalidate existing timer if there is one
// Begin a new timer from now
timer = Timer.scheduledTimer(withTimeInterval: timeInterval, repeats: false, block: { [weak self] timer in
private func handleTimer(_ timer: Timer) {
guard timer.isValid else {
handler = nil
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment