Created
November 21, 2018 00:09
-
-
Save rizumita/36d0090956518e06e19e6f48fa44446a to your computer and use it in GitHub Desktop.
SwiftでDispatchQueueでdebounce
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
extension DispatchQueue { | |
func debounce(interval: DispatchTimeInterval) -> (_ exec: @escaping () -> ()) -> DispatchSourceTimer? { | |
var timer: DispatchSourceTimer? | |
return { [weak self] exec in | |
timer?.cancel() | |
timer = DispatchSource.makeTimerSource(queue: self) | |
timer?.setEventHandler(handler: exec) | |
timer?.schedule(deadline: .now() + interval, repeating: .infinity) | |
timer?.resume() | |
return timer | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment