没有初始延迟的 NSTimer
NSTimer Without Initial Delay
有没有办法消除 NSTimer
上的初始延迟?以下代码是我的计时器,没问题。
NSTimer *timer;
timer = [NSTimer scheduledTimerWithTimeInterval:5.0 target:self selector:@selector(updateTime) userInfo:nil repeats:YES];
但是,该方法不会 运行 前 5 秒。当应用程序打开时,我希望我的方法 (updateTime
) 在以下时间轴上从 00:00:00 到 运行。
- 00:00:00 - 运行 方法
- 00:00:05 - 运行 方法
- 00:00:10 - 运行 方法
目前只有:
- 00:00:05 - 运行 方法
- 00:00:10 - 运行 方法
您不能在定时器的时间间隔结束之前触发定时器,但您可以这样做:
NSTimer *timer;
timer = [NSTimer scheduledTimerWithTimeInterval:5.0 target:self selector:@selector(updateTime) userInfo:nil repeats:YES];
[self updateTime];
只需添加这个简单的行
NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:5.0 target:self selector:@selector(updateTime) userInfo:nil repeats:YES];
[timer fire];
设置对象后立即调用 fire() Api;它将直接启动,不会延迟,从而解决您的问题。检查上传的图片以供参考。
有没有办法消除 NSTimer
上的初始延迟?以下代码是我的计时器,没问题。
NSTimer *timer;
timer = [NSTimer scheduledTimerWithTimeInterval:5.0 target:self selector:@selector(updateTime) userInfo:nil repeats:YES];
但是,该方法不会 运行 前 5 秒。当应用程序打开时,我希望我的方法 (updateTime
) 在以下时间轴上从 00:00:00 到 运行。
- 00:00:00 - 运行 方法
- 00:00:05 - 运行 方法
- 00:00:10 - 运行 方法
目前只有:
- 00:00:05 - 运行 方法
- 00:00:10 - 运行 方法
您不能在定时器的时间间隔结束之前触发定时器,但您可以这样做:
NSTimer *timer;
timer = [NSTimer scheduledTimerWithTimeInterval:5.0 target:self selector:@selector(updateTime) userInfo:nil repeats:YES];
[self updateTime];
只需添加这个简单的行
NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:5.0 target:self selector:@selector(updateTime) userInfo:nil repeats:YES];
[timer fire];
设置对象后立即调用 fire() Api;它将直接启动,不会延迟,从而解决您的问题。检查上传的图片以供参考。