Skip to content
This repository was archived by the owner on May 1, 2026. It is now read-only.

Can't figure out how to stop timer #36

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()
        }
        
    }

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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