tags:

views:

269

answers:

4

I am trying to have a block of code run 2 seconds after I 'start' it.

I think the NSTimer can do this but can't figure it out.

+1  A: 

You should be able to set the NSTimeInterval to 2.0 seconds and it should fire after that amount of time. What are you seeing? What is the code you are using to invoke the timer?

fbrereto
+3  A: 

The following will do what you need:

 NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval: 2 
                                   target:self 
                                   selector:@selector(handleTimer:) 
                                   userInfo:nil
                                   repeats:NO];

Then the delegate function:

-(void)handleTimer: (NSTimer *) timer 
{       
   //code
}
Jason Webb
+4  A: 

NSTimer can be used, but another option is to use performSelector:withObject:afterDelay: It's basically like a method call (message send) that happens later.

This example will send a doStuff: message after a delay:

[self performSelector:@selector(doStuff:) withObject:self afterDelay:2];

which causes this method to get invoked 2.0 seconds later:

-(void)doStuff:(id)sender 
{
    /// do something
}
progrmr
A: 

anyway i can sell when timer finished running?

John