您的位置:首页 > 编程语言

延长代码执行时间

2015-11-24 11:40 363 查看
方法一:

初始化:

NSTimer *timer =[NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(surplusTime) userInfo:nil repeats:YES];


移除定时器:

[timer invalidate];
timer = nil;


方法二:

[self performSelector:@selector(stop) withObject:nil afterDelay:3];


方法三:

sleep(3);延时3秒


此方式在主线程和子线程中均可执行。

是一种阻塞的执行方式,建方放到子线程中,以免卡住界面

没有找到取消执行的方法。

方法四:

GCD方式
double delayInSeconds ;
delayInSeconds = 2;
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC));
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
// 具体执行的操作
});
此方式在可以在参数中选择执行的线程。

是一种非阻塞的执行方式,

没有找到取消执行的方法。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: