ios - 没有初始延迟的 NSTimer

标签 ios objective-c nstimer

有没有办法消除 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];

关于ios - 没有初始延迟的 NSTimer,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28507113/

相关文章:

ios - 制作非可变对象的可变副本的首选方法?

ios - 触摸和移动 Sprite 跟踪效果不佳

ios - cocoa 伐木 worker : how to show file and line number?

ios - 使用 NSTimer 和动画的 UIImageView 和 UILabel 的奇怪行为

ios - 从 NSTimer 调用静态方法。可能吗?

objective-c - iOS简单循环有什么问题?

ios - 如何更改 UIActivityViewController 中的项目位置

ios - 如何在objective-c中使用cocoa pods升级magic record sdk

objective-c - 从 View Controller 观察单例的数据

objective-c - 为什么这个 NSInvocation 会引发异常?