NSTimer同时重复操作?

时间:2012-03-26 02:13:45

标签: iphone ios nstimer

以下是我的源代码,我不知道为什么。 任何人都可以向我解释一下吗?谢谢。

- (void)onTimer:(NSTimer *)timer {
    long currentPlaybackTime = self.player.currentPlaybackTime;
    int currentHours = (currentPlaybackTime / 3600);
    int currentMinutes = ((currentPlaybackTime / 60) - currentHours*60);
    int currentSeconds = (currentPlaybackTime % 60);
    self.currentLabel.text = [NSString stringWithFormat:@"%i:%02d:%02d", currentHours, currentMinutes, currentSeconds];
    if( currentMinutes > 0 && currentSeconds == 5)
    {
        //duplicate action at here.
        NSLog(@"Make a thread to decrypt file buffer");
        [self bufferEncryption];
    }
}

通话计时器:

[NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(onTimer:) userInfo:nil repeats:YES];

当我尝试:

[NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(onTimer:) userInfo:nil repeats:NO];

定时器只调用一轮。为什么?

此致

1 个答案:

答案 0 :(得分:2)

回答你的评论....你每隔半个小时调用你的计时器,所以(currentPlaybackTime % 60)将评估为5两次(mod将返回一个INTEGER)。一次为5次,再为5.5次。这就是它被调用两次的原因。