views:

65

answers:

1

Hi guys,

I am using a NSTimer instance that is called after 300 seconds. The code is something like this:

timer = [[NSTimer scheduledTimerWithTimeInterval:300 target:self selector:@selector(aMethod) userInfo:nil repeats:NO] retain];

I understand that since this is a non-repeating timer, that [timer timeInterval]; returns 0. Is there a way that I can get the elapsed time? I need to use the time that is elapsed (say 20 seconds) as when the application enters background on iOS4, the timer gets suspended and I need to get the elapsed time for the timer so I can subtract this elapsedTime + time the app was in background from the original 300 and do something if the time is exceeded.

Thanks.

A: 

Save the current time -- then the elapsed time is the current time at the point of backgrounding minus that time.

NSDate *timerStartTime = [NSDate date];

later

NSTimeInterval elapsed = [[NSDate date] timeIntervalSinceDate:timerStartTime];
Lou Franco