Last active
April 15, 2021 14:20
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
How expensive is the Timer creation?
Will it add value if notched up the implementation to make use operation queues?