首页 > 代码库 > 取消 NSTimer 和 performSelector

取消 NSTimer 和 performSelector

使用 NSTimer 和 perfrormSelectorOnMainThread:withObject:waitUntilDone 做定时并可以取消移除:

-(void)viewDidAppear:(BOOL)animated{    [super viewDidAppear:animated];    [self performSelectorOnMainThread:@selector(autoRefreshRecentDoctorAdvices) withObject:nil waitUntilDone:NO];}//自动刷新-(void)autoRefreshRecentDoctorAdvices{    //_timer 是对象的私有变量,以便取消定时    _timer = [NSTimer scheduledTimerWithTimeInterval:100 target:self selector:@selector(getRecentDoctorAdvices) userInfo:nil repeats:YES];}//取消定时,并移除 performSelector-(void)   cancelTimer{   if( _timer)    {        [_timer invalidate];        [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(autoRefreshRecentDoctorAdvices) object:nil];        return;    }  }

  

取消 NSTimer 和 performSelector