creating a timer when firing a function

Discussion in 'iOS Programming' started by ulquiorra, Sep 14, 2009.

  1. ulquiorra macrumors member

    Joined:
    Jun 17, 2009
    #1
    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?
     
  2. ulquiorra thread starter macrumors member

    Joined:
    Jun 17, 2009
    #2
    Figured out the problem I wasn't retaining the variables starttime and endttime.
     
  3. PhoneyDeveloper macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #3
    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);
     
  4. ulquiorra thread starter macrumors member

    Joined:
    Jun 17, 2009

Share This Page