Skip to content

Instantly share code, notes, and snippets.

@amfathi
Last active April 15, 2021 14:20
Show Gist options
  • Save amfathi/0dffa599f45e5ca4ca3d126472241032 to your computer and use it in GitHub Desktop.
Save amfathi/0dffa599f45e5ca4ca3d126472241032 to your computer and use it in GitHub Desktop.
[Tutorial] Naive throttling for a block from rapid execution for a specific period of time. Inspired by https://rxmarbles.com/#throttleTime
import Foundation
private var timer: Timer?
func throttle(_ interval: TimeInterval, block: @escaping (() -> Void)) {
timer?.invalidate()
timer = Timer.scheduledTimer(withTimeInterval: interval, repeats: false, block: { _ in
block()
})
}
@amfathi
Copy link
Author

amfathi commented Apr 23, 2020

How expensive is the Timer creation?
Will it add value if notched up the implementation to make use operation queues?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment