iPhone 中每 60 秒调用一个方法

发布于 2024-11-01 11:24:28 字数 214 浏览 0 评论 0原文

我创建了一个 GKSession,当它的对象被创建时,它开始搜索设备的可用性,因为

 - (void)session:(GKSession *)session peer:(NSString *)peerID didChangeState:(GKPeerConnectionState)state {

我想每 60 秒后调用这个方法,我应该做什么?

I have created an GKSession and as its object is created, it starts search for availability of devices, as

 - (void)session:(GKSession *)session peer:(NSString *)peerID didChangeState:(GKPeerConnectionState)state {

I want to call this method after each 60 seconds, what should I do?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

口干舌燥 2024-11-08 11:24:28

使用 NSTimer

NSTimer* myTimer = [NSTimer scheduledTimerWithTimeInterval: 60.0 target: self
                                   selector: @selector(callAfterSixtySecond:) userInfo: nil repeats: YES];

每 60.0 秒后,iOS 将调用以下函数

-(void) callAfterSixtySecond:(NSTimer*) t 
{
    NSLog(@"red");
}

Use NSTimer

NSTimer* myTimer = [NSTimer scheduledTimerWithTimeInterval: 60.0 target: self
                                   selector: @selector(callAfterSixtySecond:) userInfo: nil repeats: YES];

After each 60.0 second , iOS will call the below function

-(void) callAfterSixtySecond:(NSTimer*) t 
{
    NSLog(@"red");
}
尐籹人 2024-11-08 11:24:28

一旦你将 NSTimer 设置为 ScheduleWithTimeInterval,它就会立即调用它。
您可以使用

  [self performSelector:@selector(doSomething) withObject:nil afterDelay:60.0f];

Once you set NSTimer to scheduleWithTimeInterval it calls it immediately.
You can use

  [self performSelector:@selector(doSomething) withObject:nil afterDelay:60.0f];
紫南 2024-11-08 11:24:28

使用以下代码:

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

 - (void)timerFired:(NSTimer*)theTimer{
 if(condition){
       [theTimer isValid]; //recall the NSTimer
       //implement your methods
  }else{
      [theTimer invalidate]; //stop the NSTimer
 }
}

Use the following code:

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

 - (void)timerFired:(NSTimer*)theTimer{
 if(condition){
       [theTimer isValid]; //recall the NSTimer
       //implement your methods
  }else{
      [theTimer invalidate]; //stop the NSTimer
 }
}
小情绪 2024-11-08 11:24:28

斯威夫特3:

Timer.scheduledTimer(withTimeInterval: 60, repeats: true, block: { (timer) in 
print("That took a minute")
})

Swift 3:

Timer.scheduledTimer(withTimeInterval: 60, repeats: true, block: { (timer) in 
print("That took a minute")
})
旧夏天 2024-11-08 11:24:28

您可以使用调度方法...

-(void) callFunction:(CCTime)dt 
{
    NSLog(@"Calling...");
}

您可以使用此调用上面的函数...

[self schedule:@selector(callFunction:) interval:60.0f];

You can use schedular method...

-(void) callFunction:(CCTime)dt 
{
    NSLog(@"Calling...");
}

you can call above function by using this...

[self schedule:@selector(callFunction:) interval:60.0f];
神爱温柔 2024-11-08 11:24:28

斯威夫特5.0

var timer = Timer.scheduledTimer(timeInterval: 60.0, target: self, selector: #selector(updateTimer), userInfo: nil, repeats: true)

@objc func updateTimer() {
   print("1 min passed")
}

Swift 5.0

var timer = Timer.scheduledTimer(timeInterval: 60.0, target: self, selector: #selector(updateTimer), userInfo: nil, repeats: true)

@objc func updateTimer() {
   print("1 min passed")
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文