简体   繁体   English

ios scheduledTimerWithTimeInterval表示一段时间

[英]ios scheduledTimerWithTimeInterval for amount of time

I want to use scheduledTimerWithTimeInterval to do a periodic task for certain amount of time lets say one hour. 我想使用scheduledTimerWithTimeInterval来执行一段时间的定期任务,比如一小时。 but How do I implement on my code. 但是我如何在我的代码上实现。 Here is my code: 这是我的代码:

timer = [NSTimer scheduledTimerWithTimeInterval: 0.1
                                          target: self
                                        selector: @selector(doSomething:)
                                        userInfo: nil
                                         repeats: YES];

Lets assume for a minute . 让我们假设一分钟。

Here is simple scenario , 这是一个简单的场景,

int remainingCounts;
NSTimer *timer;

timer = [NSTimer scheduledTimerWithTimeInterval:1 target:(self) selector:@selector(countDown) userInfo:nil repeats:YES];
remainingCounts = 60;   // int remainingCounts ;...

Method being called for every sec. 每秒调用一次方法。

-(void)countDown {
    NSLog(@"%d", remainingCounts);
    // Do Your stuff......
    if (--remainingCounts == 0) {
        //[self dismissViewControllerAnimated:YES completion:nil]; 
        [timer invalidate];
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 自动锁定ios设备,NSTimer和scheduledTimerWithTimeInterval - autolocking ios device, NSTimer and scheduledTimerWithTimeInterval iOS 9兼容版NSTimer scheduledTimerWithTimeInterval:重复:阻止:? - iOS 9 compatible version of NSTimer scheduledTimerWithTimeInterval:repeats:block:? iOS如何传递变量代客cheduleTimerWithTimeInterval - IOS How to pass variable valet the scheduledTimerWithTimeInterval 跟踪iOS应用在后台运行的时间 - Tracking the amount of time iOS app is in background 不调用cheduledTimerWithTimeInterval - scheduledTimerWithTimeInterval is not called iOS上的CoreLocation仅在后台监视位置更改一定时间? - CoreLocation on iOS to only monitor location changes in the background for a set amount of time? 时间未定,iOS 7动画的持续时间变为0 - iOS 7 animation becomes 0 duration after undetermined amount of time iOS-在经过一定时间后如何中断异步后台操作 - iOS - How to Interrupt an Asynchronous Background Operation, After a Set Amount of Time iOS:如何在不同(定义)的时间后按顺序调用函数? - iOS: How to call a function sequentially after different (defined) amount of time? 在iOS 7后台的一定时间后执行一行代码 - Execute a line of code after a certain amount of time in the background on iOS 7
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM