PDA

View Full Version : creating a timer when firing a function




ulquiorra
Sep 14, 2009, 03:01 AM
Hello all ,

I'd like to create some sort of timer that will take the time between firing different kind of functions but I'm having no such luck so far. I assumed I should use the NSDate object and I've done this so far.

I've created several functions


-(void)startTime:(id)sender
{
starttime = [NSDate date];
NSLog(@"show me the starttime %f", starttime);
}

-(void)endTime:(id)sender
{
endtime = [NSDate date];
NSLog(@"show me the endtime %f", endtime);
}


-(void)timeInterval:(id)sender
{
NSTimeInterval interval = [ starttime timeIntervalSinceDate:endtime];
NSLog(@"let me see the timeinterval between now and then %f", interval);
}




For instance when I press a playbutton which will play a movie
I will place the starttime function


-(void)playMovie:(id)sender
{
[self startTime:(id)sender];
[self callMovie];
}



And when I press a stopbutton for the movie the stopfunction will be called as will the timeInterval function


-(IBAction)stopMovie:(id)sender
{
[self stopTime:(id)sender];
[self myMovie];

[self timeInterval:(id)sender];
}


However it doesn't seem to work. If i press the stopbutton after 3 seconds I would assume my
function timeInterval would give an output of 3 seconds. Unfortunately to no avail.
Anyone knows what's going on and what I'm doing wrong?



ulquiorra
Sep 14, 2009, 08:33 AM
Figured out the problem I wasn't retaining the variables starttime and endttime.

PhoneyDeveloper
Sep 14, 2009, 10:51 AM
You may find something like this slightly more convenient

NSTimeInterval start = [NSDate timeIntervalSinceReferenceDate];
// do stuff
NSTimeInterval end = [NSDate timeIntervalSinceReferenceDate];
NSLog(@"time = %g", (end - start) * 1000.0);

ulquiorra
Sep 15, 2009, 05:11 AM
thanks that also looks good and compact;)