How to trigger NSTimer right away?

Luc picture Luc · May 26, 2012 · Viewed 14.8k times · Source

I need to call a method when my app starts and then call the same method every 5 seconds.

My code is pretty simple:

// Call the method right away
[self updatestuff:nil]

// Set up a timer  so the method is called every 5 seconds
timer = [NSTimer scheduledTimerWithTimeInterval: 5
                                          target: self
                                        selector: @selector(updatestuff:)      
                                        userInfo: nil
                                         repeats: YES];

Is there an option for the timer so it's trigger right away and then every 5 seconds ?

Answer

MCKapur picture MCKapur · May 26, 2012

NSTimer's -fire, [timer fire]; is what you're looking for.

That will fire/immediately call the timer dismissing the time delay.