Home > AI > IOS > SwiftUI >

Count down with Timer

struct ContentView: View {
     
    @State private var timeRemaining = 100
    let timer = Timer.publish(every: 1, on: .main, in: .common).autoconnect()
     
    var body: some View {
         
        Text(String(timeRemaining))
            .onReceive(timer) { time in
                if self.timeRemaining > 0 {
                    self.timeRemaining -= 1
                }
            }
    }
}

Leave a Reply