Learn Courses My Dashboard

Timer. I don't know where to put .onReceive(timer) { input i

I don’t know where to put .onReceive(timer) { input in
I’m trying to have a button that starts the timer

let timer = Timer.publish(every: 0.250, on: .main, in: .common).autoconnect()

the let timer line is above the body line

@guyzer1954

Hi Guy,

This might help:

struct ContentView: View {
    @State private var value = 0
    var timer = Timer.publish(every: 0.250, on: .main, in: .common).autoconnect()

    var body: some View {
        VStack {
            Text("Timer value: \(value)")
        }
        .onReceive(timer, perform: { _ in
            value += 1
        })
        
    }
}

Thanks that worked great