How to disable NSTimer in objective-c

I am using nested NSTimer in the application. I have two questions.

  • How to restart the time counter in this function - (void)updateLeftTime:(NSTimer *)theTimer
  • How to kill the previous timer, because it - (void)updateLevel:(NSTimer *)theTimeralso calls the timer.

- (void)viewDidLoad {
    [super viewDidLoad];

    tmLevel=[NSTimer scheduledTimerWithTimeInterval:20.0f target:self selector:@selector(updateLevel:) userInfo:nil repeats:YES];

    tmLeftTime=[NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:@selector(updateLeftTime:) userInfo:nil repeats:YES];
}

- (void)updateLevel:(NSTimer *)theTimer {
    static int count = 1;
    count += 1;

    lblLevel.text = [NSString stringWithFormat:@"%d", count];

    tfLeftTime.text=[NSString stringWithFormat:@"%d",ANSWER_TIME];

    tmLeftTime=[[NSTimer alloc] init];
    tmLeftTime=[NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:@selector(updateLeftTime:) userInfo:nil repeats:YES];
    [self playMusic];

}
- (void)updateLeftTime:(NSTimer *)theTimer {
    static int timeCounter=1;
    timeCounter+=1;
    tfLeftTime.text=[NSString stringWithFormat:@"%d", (ANSWER_TIME-timeCounter)];
}
+5
source share
4 answers
  • Use [tmLevel invalidate]to cancel the timer schedule.
  • Remember to set it immediately tmLevel=nil(to avoid using the variable after the timer has been unplanned and Runloop has been released)
  • tmLevel , , [tmLevel invalidate] NSTimer tmLevel ( )

, , , , :

tmLeftTime=[[NSTimer alloc] init];
tmLeftTime=[NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:@selector(updateLeftTime:) userInfo:nil repeats:YES];

NSTimer, tmLeftTime... , , [NSTimer scheduledTimerWithTimeInterval:...]! NSTimer, [[NSTimer alloc] init], ( ).

,

int x = 5;
x = 12; // of course the value "5" is lost, replaced by the new value
+17

, u reset

[tmLeftTime invalidate]; 
tmLeftTime = nil;

if ([tmLeftTime isValid]){
  // the timer is valid and running, how about invalidating it
  [tmLeftTime invalidate]; 
    tmLeftTime = nil;
}
+11

How to use only one timer instead of 3?

- (void)viewDidLoad {
    [super viewDidLoad];

    tmLeftTime=[NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:@selector(updateLeftTime:) userInfo:nil repeats:YES];
}

- (void)updateLevel {
    static int count = 1;
    count += 1;

    lblLevel.text = [NSString stringWithFormat:@"%d", count];

    tfLeftTime.text=[NSString stringWithFormat:@"%d",ANSWER_TIME];

    [self playMusic];

}
- (void)updateLeftTime:(NSTimer *)theTimer {
    static int timeCounter=1;
    timeCounter+=1;
    tfLeftTime.text=[NSString stringWithFormat:@"%d", (ANSWER_TIME-timeCounter)];
    if (timeCounter >= ANSWER_TIME) {
        timeCounter = 0;
        [self updateLevel];
    }
}
0
source

Invalid timer using invalidate method in your updateLevel: method and redistribute the same timer.

[tmLevel invalidate];
tmLevel = [NSTimer scheduledTimerWithTimeInterval:20.0f target:self selector:@selector(updateLevel:) userInfo:nil repeats:YES];

And if you want to call the updateTimeLeft method: you don’t need to allocate another timer, this is a big leak, since you never release these links.

tmLeftTime = [NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:@selector(updateLeftTime:) userInfo:nil repeats:YES];

And in your updateTimeLeft: just redistribute the timer method and set the condition under which it should stop.

tmLeftTime = [NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:@selector(updateLeftTime:) userInfo:nil repeats:YES];
0
source

All Articles