Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

ulquiorra

macrumors member
Original poster
Jun 17, 2009
38
0
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

Code:
    -(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

Code:
-(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

Code:
    -(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?
 
Figured out the problem I wasn't retaining the variables starttime and endttime.
 
You may find something like this slightly more convenient

Code:
NSTimeInterval	start = [NSDate timeIntervalSinceReferenceDate];
// do stuff
NSTimeInterval	end = [NSDate timeIntervalSinceReferenceDate];
NSLog(@"time = %g", (end - start) * 1000.0);
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.