Skip to content

Can't figure out how to stop timer #36

@benpixel

Description

@benpixel

Hello!

I want to make a function that starts the timer unless there is a bool true in params (e.g. animateStatusIcon(stop: true)) - in that case I want to stop the timer. Does anyone have an idea what's going on here?

The timer starts but no matter what I do it wont stop.

func animateStatusIcon(stop: Bool? = nil){
        
        let icon = NSImage(named: "statusIcon")
        let iconActive = NSImage(named: "statusIconActive")
        
        let timer = Timer.every(0.4.seconds) {
            self.statusItem.image = icon
            Timer.after(0.2.seconds) {
                self.statusItem.image = iconActive
            }
        }
            
        if(stop == true){
            print(stop!)
            print("should stop now")
            timer.invalidate()
        } else  {
            print(stop)
            print("start")
            timer.start()
        }
        
    }

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions