Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > NSTimer - How to reset the timer?

NSTimer - How to reset the timer?
Thread Tools
Xtraz
Forum Regular
Join Date: May 2000
Status: Offline
Reply With Quote
Mar 3, 2004, 04:15 AM
 
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!
     
smeger
Mac Elite
Join Date: Sep 2000
Location: Tempe, AZ
Status: Offline
Reply With Quote
Mar 3, 2004, 04:28 AM
 
Just invalidate it and create it again.
Geekspiff - generating spiffdiddlee software since before you began paying attention.
     
Xtraz  (op)
Forum Regular
Join Date: May 2000
Status: Offline
Reply With Quote
Mar 3, 2004, 03:22 PM
 
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?
     
Angus_D
Addicted to MacNN
Join Date: Mar 2000
Location: London, UK
Status: Offline
Reply With Quote
Mar 3, 2004, 04:02 PM
 
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.
     
smeger
Mac Elite
Join Date: Sep 2000
Location: Tempe, AZ
Status: Offline
Reply With Quote
Mar 3, 2004, 04:22 PM
 
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.
     
Xtraz  (op)
Forum Regular
Join Date: May 2000
Status: Offline
Reply With Quote
Mar 3, 2004, 06:48 PM
 
Works perfectly. Thanks guys.
     
   
 
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Top
Privacy Policy
All times are GMT -4. The time now is 01:15 AM.
All contents of these forums © 1995-2017 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.8 © 2000-2017, Jelsoft Enterprises Ltd.,