Evilelement
Evilelement

Reputation: 109

Calculating seconds to millisecond NSTimer

Trying to work out where I have screwed up with trying to create a count down timer which displays seconds and milliseconds. The idea is the timer displays the count down time to an NSString which updates a UILable.

The code I currently have is

-(void)timerRun {
    if (self.timerPageView.startCountdown) {
        NSLog(@"%i",self.timerPageView.xtime);

    self.timerPageView.sec = self.timerPageView.sec - 1;

    seconds = (self.timerPageView.sec % 60) % 60 ;
    milliseconds = (self.timerPageView.sec % 60) % 1000;


    NSString *timerOutput = [NSString stringWithFormat:@"%i:%i", seconds, milliseconds];
    self.timerPageView.timerText.text = timerOutput;

    if (self.timerPageView.resetTimer == YES) {
       [self setTimer];
}
}
    else {

    }


}

-(void)setTimer{
    if (self.timerPageView.xtime == 0) {
        self.timerPageView.xtime = 60000;
    }
    self.timerPageView.sec = self.timerPageView.xtime;
    self.timerPageView.countdownTimer = [NSTimer scheduledTimerWithTimeInterval:0.01 target:self selector:@selector(timerRun) userInfo:Nil repeats:YES];
    self.timerPageView.resetTimer = NO;
}

    int seconds;
    int milliseconds;
    int minutes;
}

Anyone got any ideas what I am doing wrong?

Upvotes: 1

Views: 4841

Answers (3)

rmaddy
rmaddy

Reputation: 318934

You have a timer that will execute roughly 100 times per second (interval of 0.01).

You decrement a value by 1 each time. Therefore, your self.timerPageView.sec variable appears to be hundredths of a second.

To get the number of seconds, you need to divide this value by 100. To get the number of milliseconds, you need to multiply by 10 then modulo by 1000.

seconds = self.timerPageView.sec / 100;
milliseconds = (self.timerPageView.sec * 10) % 1000;

Update:

Also note that your timer is highly inaccurate. The timer will not repeat EXACTLY every hundredth of a second. It may only run 80 times per second or some other inexact rate.

A better approach would be to get the current time at the start. Then inside your timerRun method you get the current time again. Subtract the two numbers. This will give the actual elapsed time. Use this instead of decrementing a value each loop.

Upvotes: 4

HalR
HalR

Reputation: 11083

These calculations look pretty suspect:

seconds = (self.timerPageView.sec % 60) % 60 ;
milliseconds = (self.timerPageView.sec % 60) % 1000;

You are using int type calculations (pretty sketchy in their implementation) on a float value for seconds.

   NSUInteger seconds = (NSUInteger)(self.timerPageView.sec * 100); //convert to an int
   NSUInteger milliseconds = (NSUInteger) ((self.timerPageView.sec - seconds)* 1000.);

Upvotes: 3

progrmr
progrmr

Reputation: 77291

You set a time interval of 0.01, which is every 10 milliseconds, 0.001 is every millisecond.
Even so, NSTimer is not that accurate, you probably won't get it to work every 1 ms. It is fired from the run loop so there is latency and jitter.

Upvotes: 3

Related Questions