简体   繁体   中英

Countdown Timer not accurate

I am counting down til midnight in my app. The only problem is that the countdown timer is always off by 5 or more minutes. Its never truly close the the actual time left until midnight. I did the math and am pretty sure it is correct, but maybe I am making a stupid mistake.

-(void) timerRun
{
NSDateFormatter *formatterAmPm = [[NSDateFormatter alloc] init];
[formatterAmPm setTimeZone:[NSTimeZone timeZoneWithName:@"America/New_York"]];
[formatterAmPm setDateFormat:@"a"];

NSString *amPmString = [formatterAmPm stringFromDate:[NSDate date]];

secondsCount = secondsCount + 1;
int hours = ((12*3600)-(secondsCount))/3600;
int minutes = ((12*3600)-(secondsCount)-(hours*3600))/60;
int seconds = ((12*3600)-(secondsCount)-(hours*3600))%60;

NSString *timerOutput = [NSString stringWithFormat:@"%.2d:%.2d:%.2d", hours, minutes, seconds];
_countDownLabel.text = timerOutput;

if ((secondsCount == 0) && ([amPmString isEqualToString:@"AM"]))
{
    [_countdownTimer invalidate];
    _countdownTimer = nil;
    MainViewController *ViewController = [[MainViewController alloc] initWithNibName:@"MainViewController" bundle:nil];
    ViewController.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
    [self.navigationController pushViewController:ViewController animated:YES];
    self.sidePanelController.centerPanel = [[UINavigationController alloc] initWithRootViewController:[[MainViewController alloc] init]];

}
}

-(void) setTimer
{
NSDateFormatter *formatterHour = [[NSDateFormatter alloc] init];
[formatterHour setTimeZone:[NSTimeZone timeZoneWithName:@"America/New_York"]];
[formatterHour setDateFormat:@"HH"];

NSDateFormatter *formatterMinute = [[NSDateFormatter alloc] init];
[formatterMinute setTimeZone:[NSTimeZone timeZoneWithName:@"America/New_York"]];
[formatterMinute setDateFormat:@"mm"];

NSDateFormatter *formatterSecond = [[NSDateFormatter alloc] init];
[formatterSecond setTimeZone:[NSTimeZone timeZoneWithName:@"America/New_York"]];
[formatterSecond setDateFormat:@"ss"];

NSDateFormatter *formatterAmPm = [[NSDateFormatter alloc] init];
[formatterAmPm setTimeZone:[NSTimeZone timeZoneWithName:@"America/New_York"]];
[formatterAmPm setDateFormat:@"a"];

NSString *amPmString = [formatterAmPm stringFromDate:[NSDate date]];
NSString *hourString = [formatterHour stringFromDate:[NSDate date]];
NSString *minuteString = [formatterHour stringFromDate:[NSDate date]];
NSString *secondString = [formatterHour stringFromDate:[NSDate date]];

int hour = (([hourString intValue]) * 60 * 60);
int minute = (([minuteString intValue]) * 60);
int second = [secondString intValue];
if ([amPmString isEqualToString:@"PM"])
{
    stopTime = 12*60*60;
}
else
{
    stopTime = 24*60*60;
}

secondsCount = hour + minute + second;

secondsCount = secondsCount - stopTime;

_countdownTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self     selector:@selector(timerRun) userInfo:nil repeats:YES];
}

NSTimer is not precise, it will fire sometime around about its due time. This means you can't use it to count seconds as you are trying.

Instead what you need to do is get the actual time in your timerRun method and use that to determine the number of seconds left. You are already calling [NSDate date] to obtain the AM/PM indication, just use it to also get the current time, and then do the calculation of the seconds left.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM