Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Xcode: Why is my timer counting 2 seconds on every tick?

Hi I have a timer that should count from 12:00 minutes to 0:00 in an iphone app! But when it starts it counts like this; 11:58 11:56 11:54 11:52

it is counting 2 seconds on every tick.

this is the code in the start button code:

tid.text=[NSString stringWithFormat:@"%d:%.2d",minuter,sekunder];
timer= [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(tidklick) userInfo:nil repeats:YES];

This is the method tidklick:

-(void) tidklick
{

tiden -= 1;
sekunder = tiden % 60;
minuter= (tiden - sekunder) / 60;
tid.text=[NSString stringWithFormat:@"%d:%.2d",minuter,sekunder];

}

This is the code in the beginning..

 int tiden=720;
 int sekunder;
 int minuter;

This appears in the NSLog that I put in the method...

2012-04-26 06:41:38.562 Matte [288:f803] tiden: 719
sekunder: 59
 2012-04-26 06:41:38.607 Matte [288:f803] tiden: 718
 sekunder: 58
 2012-04-26 06:41:39.562 Matte[288:f803] tiden: 717
 sekunder: 57
 2012-04-26 06:41:39.607 Matte[288:f803] tiden: 716
  sekunder: 56
 2012-04-26 06:41:40.562 Matte [288:f803] tiden: 715
 sekunder: 55
 2012-04-26 06:41:40.607 Matte [288:f803] tiden: 714
 sekunder: 54

I have tried to make a new timer with a new name and new method, but it still counts 2.. Can it have something to do with the fact that I have alot of buttons and stuff on the screen? Some memory error?

Thanks in advance!

like image 708
Mangy92 Avatar asked Mar 26 '26 08:03

Mangy92


2 Answers

Your logic looks fine. Are you sure you’re not creating the timer twice?

like image 56
Noah Witherspoon Avatar answered Mar 27 '26 23:03

Noah Witherspoon


you code don't have problem, try logging first

then you will know if it count every 2 sec or it refresh UI at every 2 sec

-(void) tidklick
{

tiden -= 1;
sekunder = tiden % 60;
// add log here
NSLog(@"tiden: %d\n sekunder: %d",tiden,sekunder);

minuter= (tiden - sekunder) / 60;
tid.text=[NSString stringWithFormat:@"%d:%.2d",minuter,sekunder];

}

And you can try to run the timer in background thread

[self performSelectorInBackground:@selector(schedule) withObject:nil];

- (void) schedule {
NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
NSRunLoop* runLoop = [NSRunLoop currentRunLoop];

timer = [[NSTimer scheduledTimerWithTimeInterval:1.0f
                                              target:self
                                    selector:@selector(run:)
                                    userInfo:nil
                                     repeats:YES]
     retain];

[runLoop run];
[pool release];
}


-(void) run:(id) sender{

// you should handle the thread-safe 
tiden -= 1;
sekunder = tiden % 60;
// add log here
NSLog(@"tiden: %d\n sekunder: %d",tiden,sekunder);
minuter= (tiden - sekunder) / 60;
NSString *test =[NSString stringWithFormat:@"%d:%.2d",minuter,sekunder];

[label performSelectorOnMainThread:@selector(setText:) withObject:test waitUntilDone:YES];
}
like image 45
Codebear Avatar answered Mar 27 '26 23:03

Codebear



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!