Fork me on GitHub

NSTimer延迟触发

@implementation MyClass

@synthesize timer;

-(void)loadView
{
self.timer = [NSTimer scheduledTimerWithTimeInterval:0.0
target:self
selector:@selector(targetMethod:)
userInfo:nil
repeats:YES];
[self performSelector:@selector(cancelTimer) withObject:nil afterDelay:5.0];
}

-(void)cancelTimer {
[self.timer invalidate];
}

-(void)targetMethod:(NSTimer *)timer {
NSLog(@"bla");
}

 

This is short and simple:

NSDate *endtime = [NSDate dateWithTimeIntervalSinceNow:5];
[NSTimer scheduledTimerWithTimeInterval:1
target:self
selector:@selector(timerTick:)
userInfo:endtime
repeats:YES];


-(void)timerTick:(NSTimer*)timer
{
NSLog(@"timer tick");
if ( [timer.userInfo timeIntervalSinceNow] < 0 )
{
[timer invalidate];
NSLog(@"invalidating timer");
}
}



posted on 2012-02-19 18:55  pengyingh  阅读(1121)  评论(0)    收藏  举报

导航