开发者

Making a function wait a certain amount of time before it executes

I have a button which when clicked, I want it to wait 10 seconds before it does its 开发者_如何学Cthing like for example switching to a new view. How would I go about in doing this? Any help would be appreciated!


- (void)performSelector:(SEL)aSelector withObject:(id)anArgument afterDelay:(NSTimeInterval)delay

Read up on the NSObject documentation for a few other variants.

[self performSelector:@selector(myFunction:) withObject:myObject afterDelay:10.0];


Try this:

[NSTimer scheduledTimerWithTimeInterval:10.0 target:self selector:@selector(timerFired:) userInfo:nil repeats:NO];

Then timer: should look like this:

- (void)timerFired:(NSTimer *)timer {
      //do stuff here
}


You can use:

double delayInSeconds = 10.0; // number of seconds to wait
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
    /***********************
     * Your code goes here *
     ***********************/
}); 


you need to use NSTimer,

Check the below code as reference.

- (void) startTimer{

    [NSTimer scheduledTimerWithInterval:10.0f target:self selector:@selector(showElapsedTime:) userInfo:nil repeats:YES];
}

showElapsedTime will be called after delay, you provide.

-(void) showElapsedTime: (NSTimer *) timer {

    if(OnSomeCondition)
    {
      [timer invalidate];
    }

// Write your code here 
}

Call StartTimer from your action method of your UIButton, you will get 10 second wait.

-(void) myButtonAction:(id) sender
{
   [self StartTimer]; 
}


You could do a

sleep(10)

to make the app pause for 10 seconds. Note: This is a real, real pause, so no UI interaction is possible at all. Pressing the Home button will work and move your app to the background, though.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜