-1

Does any one want to teach me how to using loop process using NSTimer? I have 2 process timer to do, timer1 and timer2 repeated to n loop (timer1 -> timer2 -> timer1 -> timer2 and so on until n loop) that are triggered by a button. I am new to xcode. please teach me. both timer has input by user. please give me example if possible.

My code should be like this. Corret me if i'm wrong

- (void) timer2Elapsed:(NSTimer *) timer;
{
    ...

    displayCountDown.text = [NSString stringWithFormat:@"%d : %d : %d", hour , minute, second];
}

- (void)timer1Elapsed: (NSTimer *) timer;
{
    ...

    displayCountDown.text = [NSString stringWithFormat:@"%d : %d : %d", hour , minute, second];
}

My button who trigger the countdown:

- (IBAction)startBtn:(id)sender {
        endSetTime = [NSDate timeIntervalSinceReferenceDate] + totalSecondTime;
        endSetRest = [NSDate timeIntervalSinceReferenceDate] + totalSecondRest;
        countdownTimer = [NSTimer scheduledTimerWithTimeInterval: 0.99 target: self selector: @selector(timer1Elapsed:) userInfo: nil repeats: YES];

}

someone here told me to using this code but i don't know what should i write inside and where to put it in? He said for looping? I don't know? And how to connect that code inside my button?

+ (void) startTimer:(id)timer
{
    static int numberOfTimes = 0;
    if(numberOfTimes >= 5)
    {
        numberOfTimes = 0;
        return;
    }
    numberOfTimes ++;

    [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(timer1Elapsed:) userInfo:nil repeats:NO];
}
Alan Haggai Alavi
  • 72,802
  • 19
  • 102
  • 127
Piyo
  • 181
  • 1
  • 2
  • 9

1 Answers1

0

Forget timers. Use performSelector:withObject:afterDelay: selector.

@interface CECountdown : NSObject {
    NSDate *_startDate;
    NSTimeInterval _countdownInterval;
}

@end

@implementation CECountdown

- (id)initWithCountdownInterval:(NSTimeInterval)interval {
    self = [super init];

    if(self) {
        _countdownInterval = interval;
        _startDate = [[NSDate date] retain];
    }

    return self;
}

- (void)dealloc {
    [_startDate release];

    [super dealloc];
}

- (void)method0 {
    // Do something
    [self performSelector:@selector(method1) withObject:nil afterDelay:0.3];
    [self checkTimeout];
}

- (void)method1 {
    // Do something
    [self performSelector:@selector(method0) withObject:nil afterDelay:0.3];    
    [self checkTimeout];
}

- (void)invalidate {
    [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(method1) object:nil];
    [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(method2) object:nil];
}

- (void)checkTimeout {
    if([NSDate timeIntervalSinceReferenceDate] - [_startDate timeIntervalSinceReferenceDate] > _countdownInterval) {
        [self invalidate];
    }
}

- (void)didTapStartButton:(id)sender {
    [self performSelector:@selector(method1) withObject:nil afterDelay:0.3];
}

@end
Cemal Eker
  • 1,266
  • 1
  • 9
  • 24
  • im sorry, is there any chance loop using nstimer? because if i using performSelector, i don't know how to change code inside my NSTimer. Inside my NSTimer is like this : – Piyo Jun 27 '12 at 08:49
  • NSTimeInterval remainingTime = endSetRest - [NSDate timeIntervalSinceReferenceDate]; NSTimeInterval scratch; if (remainingTime <= 0) { [timer invalidate]; remainingTime = 0; } hour = remainingTime / 3600; scratch = fmod(remainingTime,3600); minute = scratch / 60; second = fmod(scratch,60); displayCountDown.text = [NSString stringWithFormat:@"%d : %d : %d", hour , minute, second]; – Piyo Jun 27 '12 at 08:50
  • You can still calculate remaining time by storing start time in a instance variable. I don't understand what is not doable with this method. I don't think it's possible to loop with NSTimers the way you wanted. – Cemal Eker Jun 27 '12 at 10:51
  • hey, how to use invalidate? they countdown to negative number. where should i put that invalidate? i want invalidate if they reach 0. again, is your code right? why in method1 using @selector(method1)? why not @selector(method2)? and at method0, you using @selector(method2)? my time countdown only at method1 but it cant continue to your method0, why? and seems the time tick sometimes has delay? – Piyo Jun 27 '12 at 11:48
  • im sorry, i already using your code but it still not right. your code is calling 2 process, run it together that is method0 and method1 and i think you forget to write for loop process because i don't see any loop for "x" (just say 5) times. But thank you, at least you bring me to the nearest one. I just think maybe my case havent yet tried by some people. – Piyo Jun 28 '12 at 10:01
  • You could count loops with an instance variable. For example if you need to loop for max 5 times you should add an instance variable, lets say `_loopCount`. First initialize it to zero in init method `_loopCount = 0` than increment it in each method `_loopCount += 1`. You can check for invalidation in `checkTimeout` method, `if(_loopCount >= 5) {[self invalidate]}` – Cemal Eker Jun 28 '12 at 11:10