Countdown Not Ideal. A Simple Fix?

Discussion in 'Mac Programming' started by Darkroom, Dec 11, 2008.

  1. Darkroom Guest

    Darkroom

    Joined:
    Dec 15, 2006
    Location:
    Montréal, Canada
    #1
    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"?

    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;
    		}
    	}
    
     
  2. iSee macrumors 68040

    iSee

    Joined:
    Oct 25, 2004
    #2
    I think what's causing trouble is that "theInterval" is expressing the time that has passed but you want to output the time remaining.

    So I'd do something like this, where you calculate theHours, theMinutes, etc. from the total seconds remaining

    Code:
    ...
    int hoursSelected = ([self timeMenuSelection] * 3600); 
    
    int totalSecondsRemaining = hoursSelected - theInterval;
    
    int theHours = totalSecondsRemaining / 3600;
    int theMinutes = (totalSecondsRemaining / 60) % 60;
    int theSeconds = totalSecondsRemaining % 60;
    
    
    Edit 2: Also, I think the variable name "hoursSelected" is confusing. It looks like it is actually the number of seconds in the total time to wait. So it actually might be better named "secondsSelected."
     

Share This Page