我需要在应用程序启动时调用方法,然后每隔5秒调用相同的方法.
我的代码非常简单:
// 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];
Run Code Online (Sandbox Code Playgroud)
是否有计时器的选项,所以它立即触发,然后每5秒触发一次?
小智 17
您无法在同一行代码中安排计时器并立即触发事件.所以你必须在两行代码中完成它.
首先,安排计时器,然后立即调用计时器上的'fire':
timer = [NSTimer scheduledTimerWithTimeInterval: 5
target: self
selector: @selector(updatestuff:)
userInfo: nil
repeats: YES];
[timer fire];
Run Code Online (Sandbox Code Playgroud)
当调用fire时,计时器等待触发的时间间隔将重置,因为它刚刚运行,然后将在指定的时间间隔后继续运行 - 在这种情况下,每5秒一次.
ini*_*333 11
在斯威夫特:
timer = Timer.scheduledTimer(timeInterval: interval, target: self, selector: #selector(foo), userInfo: nil, repeats: true)
timer.fire()
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
13381 次 |
| 最近记录: |