Question

I am wondering if there is some way that I can create a timer that countdown from a given time. For example, say I want this timer to last an hour. There will be a NSTextField that will show the time remaining (ex. 25 minutes), and will auto update every minute to the new value. And then, when an hour is finally passed, it will run some function. I have seen people suggesting NSTimer and NSDate for this, but am wondering what you all could suggest.

Thanks!

EDIT: My current code (timeInstance is an instance variable):

- (void)awakeFromNib:
{
timeInstance = [[NSDate date] addTimeInterval:(10 * 60)];
[timeInstance retain];

[timer invalidate];
timer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(timer:) userInfo:NULL repeats:YES];
}

- (void)timer:(NSTimer *)myTimer
{
NSDate *now = [NSDate date];
// Compare
}
Was it helpful?

Solution

NSTimer and NSDate sounds perfectly reasonable.

EDIT: As a side note, it might be a good idea to increase the frequency as the target time approaches; allowing you to change from hour display to minute display to second display.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top