TorbenVerdorben
TorbenVerdorben

Reputation: 159

NSTimer repeats way to fast

Hallo i have a question about the NSTimer.scheduledTimerWithTimeInterval. I set it up with an interval of two- however it repeats way more then every 2 seconds. Does anyone know what i do wrong?

NSTimer.scheduledTimerWithTimeInterval(2.0, target: self, selector: Selector("updateAllHuds"), userInfo: nil, repeats: true)


func updateAllHuds(){
    self.starTimer-=1
    self.builtTimer-=1
    self.gravityTimer-=1

    if self.powerStar.text == "1"{
        self.powerStar.removeFromParent()
        self.starActive = false
        self.activePowers-=1
        self.star.removeFromParent()
    }

...

Upvotes: 2

Views: 379

Answers (1)

Vincent Ritter
Vincent Ritter

Reputation: 21

Looking at your code you seem to update ever 1 second:

NSTimer.scheduledTimerWithTimeInterval(1.0, target: self, selector: Selector("updateAllHuds"), userInfo: nil, repeats: true)

If you look at the docs you can see the first parameter "seconds" for the interval:

The number of seconds between firings of the timer. If seconds is less than or equal to 0.0, this method chooses the nonnegative value of 0.1 milliseconds instead.

I would set this to 2 seconds as such:

NSTimer.scheduledTimerWithTimeInterval(2.0, target: self, selector: Selector("updateAllHuds"), userInfo: nil, repeats: true)

Hope that helps for the NSTimer schedule.

Upvotes: 2

Related Questions