使用 NSTimer 循环 [英] while loop using NSTimer

查看:51
本文介绍了使用 NSTimer 循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个运行时间为 10 分钟的 NSTimer.然后我想写一个 while 循环,在执行之后的行之前延迟 10 分钟.例如.

I want to create a NSTimer that runs for lets say 10 minutes. I then want to write a while loop aftewards delaying 10 minutes of time before the line afterwards is executed. For example.

     NSTimer * countDown = [NSTimer scheduledTimerWithTimeInterval:10.0 target:self selector:@selector userInfo:nil repeats:NO];

while (countDown == stil running (hasnt reached 10 minute mark) ){
// kill 10 minutes of time
}//when 10 minutes is up

execute next line of code

推荐答案

第一

scheduledTimerWithTimeInterval: 方法的 timeInterval 参数以秒为单位.如果你想在几分钟内,不要忘记乘以60.

The timeInterval parameter of scheduledTimerWithTimeInterval: method is in seconds. If you want in minutes, don't forget to multiply by 60.

第二

你为什么要等那么一会儿倒计时?只需在 NSTimer 触发的选择器中调用您想在 10 分钟后执行的行.

Why would you want to wait the countDown with a while like that? Just call the lines you want to execute 10 minutes later in the selector that NSTimer fires.

NSTimer * countDown = [NSTimer 
                        scheduledTimerWithTimeInterval:(10.0 * 60)
                        target:self 
                        selector:@selector(tenMinutesLater) 
                        userInfo:nil 
                        repeats:NO];

然后

-(void)tenMinutesLater
{
   //put some code here. This will be executed 10 minutes later the NSTimer was initialized
}

这篇关于使用 NSTimer 循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆