this code works with counting down, but if the user selects "1 Hour", the output will start at "Time Remaining: 00 Hours 00 Minutes 59 Seconds". when the first minute passes, it will read "Time Remaining: 00 Hours 59 Minutes 59 Seconds". the timer will stop one minute earlier than expected at "Time Remaining: 00 Hours 01 Minutes 00 Seconds", since the last minute seem to be added to the beginning of the timer.
how can i make it display correctly starting at "Time Remaining: 00 Hours 59 Minutes 59 Seconds" and finishing at "Time Remaining: 00 Hours 00 Minutes 00 Seconds"?
how can i make it display correctly starting at "Time Remaining: 00 Hours 59 Minutes 59 Seconds" and finishing at "Time Remaining: 00 Hours 00 Minutes 00 Seconds"?
Code:
-(int)timeMenuSelection
{
return [[menu selectedItem] tag];
}
- (IBAction)startTimer:(id)sender
{
startTime = [NSDate timeIntervalSinceReferenceDate];
[killTimer invalidate];
[killTimer release];
killTimer = nil;
killTimer = [[NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(updateTime:) userInfo:nil repeats:YES] retain];
}
- (void)updateTime:(NSTimer *)theTimer
{
NSTimeInterval now = [NSDate timeIntervalSinceReferenceDate];
NSTimeInterval interval = now - startTime;
int theInterval = (int)interval;
[COLOR="SeaGreen"]//
// //Tag #1 x 3600 Seconds = 3600 Seconds = 1 Hours.
// //Tag #2 x 3600 Seconds = 7200 Seconds = 2 Hours.
// //Tag #3 x 3600 Seconds = 10800 Seconds = 3 Hours.
//[/COLOR]
int hoursSelected = ([self timeMenuSelection] * 3600);
int theHours = (hoursSelected-theInterval)/3600;
int theMinutes = (hoursSelected*60-theInterval/60)%60;
int theSeconds = (hoursSelected*3600-theInterval)%60;
if (theInterval <= hoursSelected)
{
NSLog(@"Time Remaining: %.2d Hours %.2d Minutes %.2d Seconds", theHours, theMinutes, theSeconds);
}
else
{
NSLog(@"TIME'S UP!");
[killTimer invalidate];
[killTimer release];
killTimer = nil;
}
}