while循环使用NSTimer

我想创build一个运行的NSTimer可以说10分钟。 然后我想在延迟10分钟的时间之后写一个while循环,之后执行这行。 例如。

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

第二

你为什么要这样等待呢? 只需要在NSTimer触发的select器中,在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 } 

而不是一个while循环,只需在第一个定时器所调用的方法内创build一个新的定时器即可。

  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秒。