• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Killing a NSTimer
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Killing a NSTimer


  • Subject: Re: Killing a NSTimer
  • From: Eric Brunstad <email@hidden>
  • Date: Sat, 11 Jun 2005 20:38:43 -0400


On Jun 11, 2005, at 1:58 PM, Jerry Brace wrote:

I'm using an NSTimer for an event which fires once after it is set (Repeats:NO).

I'd like to know how to cancel/remove this timer in another event.

Ex: I click "Snooze" to start the timer and then I click the "Close" before the snooze timer fires. I want the close button to kill the snooze timer.

    NSTimer * snoozeTimer = [NSTimer timerWithTimeInterval:snoozeSeconds
    target:self
    selector:@selector(snoozeControl)
    userInfo:nil
    repeats:NO];
    [[NSRunLoop currentRunLoop] addTimer:snoozeTimer forMode:NSDefaultRunLoopMode];


Thanks!
Jerry

Make snoozeTimer an instance variable and call [snoozeTimer invalidate] to stop the timer from the clost button.

Like this:

- (id)init
{
    self = [super init];
    if (self)
    {
    
    // not really required
    snoozeTimer = nil;
    }
    return self;
}

- (IBAction) snooze:(id)sender
{
    // If we're already snoozing then do nothing
    if (snoozeTimer !=nil) return;
    
    // create a timer and fire it
    snoozeTimer = [NSTimer scheduledTimerWithTimeInterval:snoozeSeconds
            selector:@selector(snoozeControl:)
            userInfo:nil]
            repeats:NO];
}

- (IBAction) wakeUp:(id)sender
{
    // if it's already stopped do nothing
    if (snoozeTimer == nil) return;
    
    // stop the timer
    [snoozeTimer invalidate];

    // set it back to nil
    snoozeTimer = nil;
}

- (void) snoozeControl:(NSTimer *)sender
{
    // your implementation of snoozeControl here
    NSRunAlertPanel(@"Wake Up",@"Nap time's over",nil,nil,nil);
}

Eric Brunstad
Mind Sprockets Software
email@hidden
www.mindsprockets.com
 _______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:

This email sent to email@hidden

References: 
 >Killing a NSTimer (From: Jerry Brace <email@hidden>)

  • Prev by Date: Re: NSMutableDictionary setObject:forKey:
  • Next by Date: Re: IBOutlet & NSTabView
  • Previous by thread: Re: Killing a NSTimer
  • Next by thread: Looking for a mentor
  • Index(es):
    • Date
    • Thread