1

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
4

2 回答 2

4

第一的

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

第二

你为什么要像那样等待倒计时呢?NSTimer只需在触发的选择器中调用您想要在 10 分钟后执行的行。

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
}
于 2013-09-07T23:08:15.640 回答
0

而不是while循环,只需在第一个计时器调用的方法中创建一个新计时器。

 NSTimer *countDown = [NSTimer scheduledTimerWithTimeInterval:600 target:self selector:@selector(startSecondTimer) userInfo:nil repeats:NO];


- (void)startSecondTimer
{
    NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:600 target:self selector:@selector(doSomething) userInfo:nil repeats:NO];
}

PS,时间间隔以秒为单位 - 10 分钟 = 600 秒,而不是 10 秒。

于 2013-09-07T23:08:51.297 回答