Timer publisher init timer after button click

Jason Krowl

In all tutorials and in official documentation I only see initialization of timer straight up when the view loads.

@State private var timer = Timer.publish(every: 1, on: .main, in: .common).autoconnect()

and later on

.onReceive(timer) {....}

but how should I init timer only on button click and assign it to the unassigned / not connected timer.

Later on I will need to cancel and reconnect, but that is not the issue here. Issue here is connecting only after button click.

I tried to init it like this

@State private var timer: Cancellable?

....

timer = Timer.publish(every: 1, on: .main, in: .common).connect()

But I can not call onReceive on timer inited like this, because first:

Protocol 'Cancellable' as a type cannot conform to 'Publisher'

and second

Argument type 'Cancellable?' does not conform to expected type 'Cancellable'
malhal

Just put the timer in a child view and control its visibility with a bool. When the TimerView is removed the state is destroyed and the timer stops.

struct ContentView: View {
    @State var started = false

    var body: some View {
        VStack {
            Button(started ? "Stop" : "Start") {
                started.toggle()
            }
            if started {
                TimerView()
            }
        }
    }
}

struct TimerView: View {

    @State private var timer = Timer.publish(every: 1, on: .main, in: .common).autoconnect()

    ...

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Create a Timer Publisher using Swift Combine

Display new timer on button click in JavaScript using Moment.js

JS button click counter with timer and input-select form

Javascript cannot display countdown timer which also starts on button click

After button click timer should start again

Start timer on button click not working Javascript

A click to start the countdown timer and a second one to Pause the timer in the same button

Swift Combine: Using timer publisher in an observable object

Dynamic button click event within a timer tick event

How to set timer on click?

Change Time on timer upon click of a button

Do you need to call init_timer() again after a del_timer()

set timer to slidedown and the button change after slideup

Timer event is not triggering button click

How to stop timer affacted by button click?

Is it possible to add a timer to a button?

Javascript timer: resume button

how to get the value of the count timer on button click?

Starting timer on button click in swift

javascript countdown timer, interval speeds up after click event

Tkinter timer to start at 0 on button click

Set a timer for the visibility of a button

My Timer is not reset when click on reset button C#

Timer publisher with initial fire now

stop the timer which is executed after button click

How to get the time in seconds after click on submit button using React Count Down Timer

How to make only one button disabled on click in map method and how to change className after the timer expires in React?

Pause the timer with a button

Restart timer after button is clicked