Search code examples
iosobjective-cnstimer

NSTimer Without Initial Delay


Is there a way to remove the initial delay on an NSTimer? The following code is for my timer, which is all fine.

NSTimer *timer;
timer = [NSTimer scheduledTimerWithTimeInterval:5.0 target:self selector:@selector(updateTime) userInfo:nil repeats:YES];

However, the method does not run for the first 5 seconds. I would like my method (updateTime) to run on the following timeline from 00:00:00 when the app is opened.

  • 00:00:00 - Run Method
  • 00:00:05 - Run Method
  • 00:00:10 - Run Method

Currently, it only does:

  • 00:00:05 - Run Method
  • 00:00:10 - Run Method

Solution

  • You can't make a timer fire before its time interval has elapsed, but you can do this:

    NSTimer *timer;
    timer = [NSTimer scheduledTimerWithTimeInterval:5.0 target:self selector:@selector(updateTime) userInfo:nil repeats:YES];
    [self updateTime];