Last active
November 5, 2021 12:46
-
-
Save tomisacat/5699fd2cf8a18e69c31498f94787fae4 to your computer and use it in GitHub Desktop.
A simple count down timer using RxSwift
This file contains hidden or 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 RxSwift | |
func count(from: Int, to: Int, quickStart: Bool) -> Observable<Int> { | |
return Observable<Int> | |
.timer(quickStart ? 0 : 1, period: 1, scheduler: MainScheduler.instance) | |
.take(from - to + 1) | |
.map { from - $0 } | |
} |
@tomisacat it works fine, but how can I pause timer and resume?
how to count down in background mode
FYI, With the latest RxSwift the last map return () and not the elapsed time
By the way, if someone wants to do something when the timer elapses, try this:
func count(from: Int, to: Int, quickStart: Bool) -> Observable<Int> {
return Observable<Int>
.timer(quickStart ? 0 : 1, period: 1, scheduler: MainScheduler.instance)
.take(from - to + 1)
.map { from - $0 }
.do(onCompleted: {
// do anything when the timer elapses
})
}
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Simple count down timer which will emit element from value
from
to valueto
.