如何安排一个嵌套计时器



现在,我有一个NSDate数组,如"00:00.00","00:05.00","00:15.00"等。我已经算出了NSTimerInterval,所以可以算出scheduledTimerWithTimeInterval。但是如何安排一个嵌套的计时器,比如5秒后安排另一个15秒的计时器做一些事情,然后等等,直到日期数组结束。每个NSTimerInterval都是从2个对象的NSArray中计算出来的。我可以像这样得到第一个:

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setLocale:[NSLocale currentLocale]];
[dateFormatter setDateFormat:@"mm:ss.SS"];
NSDate *first = [dateFormatter dateFromString:[durationArray[0]]];
NSDate *second = [dateFormatter dateFromString:durationArray[1]];
NSTimeInterval timeInterval = [second timeIntervalSinceDate:first];
[NSTimer scheduledTimerWithTimeInterval:timeInterval target:self selector:@selector(goToNextLine) userInfo:nil repeats:NO];

如果我做一个for循环,计时器不会一个接一个地触发:

for (int i = 0; i<[durationArray count]; i++) {
    int j = i+1;
    if (j == [durationArray count]) {
    } else
    {
        NSDate *first = [dateFormatter dateFromString:durationArray[i]];
        NSDate *second = [dateFormatter dateFromString:durationArray[j]];
        NSTimeInterval timeInterval = [second timeIntervalSinceDate:first];
        [NSTimer scheduledTimerWithTimeInterval:timeInterval target:self selector:@selector(goToNextLine) userInfo:nil repeats:NO];
    }
}
[NSTimer scheduledTimerWithTimeInterval:timeInterval target:self selector:@selector(goToNextLine) userInfo:nil repeats:YES];

希望这将帮助您goToNextLine方法将执行时间间隔

最新更新