|
|
NSTimer - How to reset the timer?
|
|
|
|
Forum Regular
Join Date: May 2000
Status:
Offline
|
|
Hi I'd like my timer to be reset (to its maximum value) whenever a button is pressed. For example on first press the timer is at 5 seconds,
-(void) buttonPressed
{
timer = [NSTimer scheduledTimerWithTimeInterval:5.0
target:self
selector:@selector(endTimer
userInfo:nil
repeats:NO];
}
But, say 3 seconds later, (2 seconds til time is up) I press the button again it doesn't reset the timer. How do I solve this problem?
Just curious, how would you add to the remaining time of the timer (ie. 3 + 5 = 8 seconds after second press) instead? I'm sure I'm missing the point of NSTimer or something, but it just seems like watchdog timers on microcontrollers are more versatile than it.
Thanks!
|
|
|
|
|
|
|
|
|
Mac Elite
Join Date: Sep 2000
Location: Tempe, AZ
Status:
Offline
|
|
Just invalidate it and create it again.
|
Geekspiff - generating spiffdiddlee software since before you began paying attention.
|
|
|
|
|
|
|
|
Forum Regular
Join Date: May 2000
Status:
Offline
|
|
Thanks. It worked except for the case that the timer expired by itself before the button is pressed, in which case [timer invalidate] would cause an error because it had already released itself, so I just made a BOOL is_timer_active to keep track of that.
Again just curiously, to actually add say 5 seconds to the timer's remaining time, would I have to add 5 seconds to [timer fireDate] and then use set a new fireDate with that?
|
|
|
|
|
|
|
|
|
Addicted to MacNN
Join Date: Mar 2000
Location: London, UK
Status:
Offline
|
|
Originally posted by Xtraz:
Thanks. It worked except for the case that the timer expired by itself before the button is pressed, in which case [timer invalidate] would cause an error because it had already released itself, so I just made a BOOL is_timer_active to keep track of that.
You could just set the pointer to nil from the method the timer fires, then when you call [timer invalidate] it'll just work because messaging nil is allowed.
Again just curiously, to actually add say 5 seconds to the timer's remaining time, would I have to add 5 seconds to [timer fireDate] and then use set a new fireDate with that?
I think so, yes.
|
|
|
|
|
|
|
|
|
Mac Elite
Join Date: Sep 2000
Location: Tempe, AZ
Status:
Offline
|
|
Yeah, as Angus said, make your NSTimer instance variable a class variable (defined in your @interface section). When you invalidate it, set it to nil immediately afterwards. Then, you can safely invalidate it again since Cocoa internally converts messaging a nil-object to a "no operation".
Must be early in the morning for me - I can't seem to explain this very clearly.
|
Geekspiff - generating spiffdiddlee software since before you began paying attention.
|
|
|
|
|
|
|
|
Forum Regular
Join Date: May 2000
Status:
Offline
|
|
Works perfectly. Thanks guys.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Forum Rules
|
|
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
|
HTML code is Off
|
|
|
|
|
|
|
|
|
|
|
|