6

I want to use scheduledTimerWithTimeInterval to do a periodic task for certain amount of time lets say one hour. but How do I implement on my code. Here is my code:

timer = [NSTimer scheduledTimerWithTimeInterval: 0.1
                                          target: self
                                        selector: @selector(doSomething:)
                                        userInfo: nil
                                         repeats: YES];
Gabriel Tomitsuka
  • 1,121
  • 10
  • 24
HelenaM
  • 1,807
  • 6
  • 30
  • 41

1 Answers1

6

Lets assume for a minute .

Here is simple scenario ,

int remainingCounts;
NSTimer *timer;

timer = [NSTimer scheduledTimerWithTimeInterval:1 target:(self) selector:@selector(countDown) userInfo:nil repeats:YES];
remainingCounts = 60;   // int remainingCounts ;...

Method being called for every sec.

-(void)countDown {
    NSLog(@"%d", remainingCounts);
    // Do Your stuff......
    if (--remainingCounts == 0) {
        //[self dismissViewControllerAnimated:YES completion:nil]; 
        [timer invalidate];
    }
}
Poshi
  • 5,332
  • 3
  • 15
  • 32
Kumar KL
  • 15,315
  • 9
  • 38
  • 60