Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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
like image 416
simbo64 Avatar asked Jul 30 '26 21:07

simbo64


1 Answers

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];
like image 54
Caleb Avatar answered Aug 01 '26 09:08

Caleb



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!