How can I debounce a method call?

Here’s an option for those not wanting to create classes/extensions:

Somewhere in your code:

var debounce_timer:Timer?

And in places you want to do the debounce:

debounce_timer?.invalidate()
debounce_timer = Timer.scheduledTimer(withTimeInterval: 1.0, repeats: false) { _ in 
    print ("Debounce this...") 
}

Leave a Comment