定时器的时间单位是秒.
NSTimer *timer
1.运行一次自动停止
timer = [NSTimer scheduledTimerWithTimeInterval:3.0 target:self selector:@selector(function:) userInfo:nil repeats:NO];
这样timer运行一次就会自动停下来的
2.重复运行在方法中停止
timer = [NSTimer scheduledTimerWithTimeInterval:3.0 target:self selector:@selector(function:) userInfo:nil repeats:YES];
就会每三秒运行一次function:
在function:中加入 [timer invalidate];语句可在内部令其停止
NSTimer *timer
1.运行一次自动停止
timer = [NSTimer scheduledTimerWithTimeInterval:3.0 target:self selector:@selector(function:) userInfo:nil repeats:NO];
这样timer运行一次就会自动停下来的
2.重复运行在方法中停止
timer = [NSTimer scheduledTimerWithTimeInterval:3.0 target:self selector:@selector(function:) userInfo:nil repeats:YES];
就会每三秒运行一次function:
在function:中加入 [timer invalidate];语句可在内部令其停止
本文介绍了如何使用NSTimer创建定时任务,包括一次性运行和周期性执行的方式,并解释了如何通过invalidate方法停止定时器。
1236

被折叠的 条评论
为什么被折叠?



