Objective c 目标C:NStimer-如何在n小时后停止计时器
我已使用下面的调用启动计时器,需要在n小时后停止Objective c 目标C:NStimer-如何在n小时后停止计时器,objective-c,timer,nstimer,Objective C,Timer,Nstimer,我已使用下面的调用启动计时器,需要在n小时后停止 self.timer = [NSTimer scheduledTimerWithTimeInterval:20 target:self selector:@selector(sendLocationUpdates) userInfo:nil repeats:YES]; 解决方案我可以得到当前时间时,计时器启动,并不断增加时间,直到达到阈值。或者有没有更好的停止计时器的方法?简单的解决方案 声明两个属性 @property NSInteger c
self.timer = [NSTimer scheduledTimerWithTimeInterval:20 target:self selector:@selector(sendLocationUpdates) userInfo:nil repeats:YES];
解决方案我可以得到当前时间时,计时器启动,并不断增加时间,直到达到阈值。或者有没有更好的停止计时器的方法?简单的解决方案
声明两个属性
@property NSInteger counter;
@property NSTimeInterval interval;
启动计时器前,将计数器设置为0
self.counter = 0;
self.interval = 20.0;
self.timer = [NSTimer scheduledTimerWithTimeInterval:self.interval target:self selector:@selector(sendLocationUpdates) userInfo:nil repeats:YES];
在sendLocationUpdates
方法中,递增计数器
- (void)sendLocationUpdates
{
counter++;
if (counter == 4 * (3600 / self.interval)) {
[self.timer invalidate];
self.timer = nil;
}
// do other stuff
}
给定的时间间隔为20秒,计时器每小时触发180次。
在本例中,计时器在4小时后停止
声明两个属性
@property NSInteger counter;
@property NSTimeInterval interval;
启动计时器前,将计数器设置为0
self.counter = 0;
self.interval = 20.0;
self.timer = [NSTimer scheduledTimerWithTimeInterval:self.interval target:self selector:@selector(sendLocationUpdates) userInfo:nil repeats:YES];
在sendLocationUpdates
方法中,递增计数器
- (void)sendLocationUpdates
{
counter++;
if (counter == 4 * (3600 / self.interval)) {
[self.timer invalidate];
self.timer = nil;
}
// do other stuff
}
给定的时间间隔为20秒,计时器每小时触发180次。
在本例中,计时器在4小时后停止
声明两个属性
@property NSInteger counter;
@property NSTimeInterval interval;
启动计时器前,将计数器设置为0
self.counter = 0;
self.interval = 20.0;
self.timer = [NSTimer scheduledTimerWithTimeInterval:self.interval target:self selector:@selector(sendLocationUpdates) userInfo:nil repeats:YES];
在sendLocationUpdates
方法中,递增计数器
- (void)sendLocationUpdates
{
counter++;
if (counter == 4 * (3600 / self.interval)) {
[self.timer invalidate];
self.timer = nil;
}
// do other stuff
}
给定的时间间隔为20秒,计时器每小时触发180次。
在本例中,计时器在4小时后停止
声明两个属性
@property NSInteger counter;
@property NSTimeInterval interval;
启动计时器前,将计数器设置为0
self.counter = 0;
self.interval = 20.0;
self.timer = [NSTimer scheduledTimerWithTimeInterval:self.interval target:self selector:@selector(sendLocationUpdates) userInfo:nil repeats:YES];
在sendLocationUpdates
方法中,递增计数器
- (void)sendLocationUpdates
{
counter++;
if (counter == 4 * (3600 / self.interval)) {
[self.timer invalidate];
self.timer = nil;
}
// do other stuff
}
给定的时间间隔为20秒,计时器每小时触发180次。
在本例中,计时器在4小时后停止将实例变量添加到类中以存储计时器的开始时间:
self.timer = [NSTimer scheduledTimerWithTimeInterval:20 target:self selector:@selector(sendLocationUpdates) userInfo:nil repeats:YES];
_startTime = [NSDate timeIntervalSinceReferenceDate];
YourClass.m:
@interface YourClass () {
NSTimeInterval _startTime;
}
@end
创建计时器时记录当前时间:
self.timer = [NSTimer scheduledTimerWithTimeInterval:20 target:self selector:@selector(sendLocationUpdates) userInfo:nil repeats:YES];
_startTime = [NSDate timeIntervalSinceReferenceDate];
并在sendLocationUpdates
方法中测试当前时间:
#define TIMER_LIFE_IN_SECONDS 3000.0
- (void)sendLocationUpdates
{
// do thing
NSTimeInterval now = [NSDate timeIntervalSinceReferenceDate];
if (now - _startTime > TIMER_LIFE_IN_SECONDS) {
[self.timer invalidate];
self.timer = nil;
}
}
向类中添加实例变量以存储计时器的开始时间:
self.timer = [NSTimer scheduledTimerWithTimeInterval:20 target:self selector:@selector(sendLocationUpdates) userInfo:nil repeats:YES];
_startTime = [NSDate timeIntervalSinceReferenceDate];
YourClass.m:
@interface YourClass () {
NSTimeInterval _startTime;
}
@end
创建计时器时记录当前时间:
self.timer = [NSTimer scheduledTimerWithTimeInterval:20 target:self selector:@selector(sendLocationUpdates) userInfo:nil repeats:YES];
_startTime = [NSDate timeIntervalSinceReferenceDate];
并在sendLocationUpdates
方法中测试当前时间:
#define TIMER_LIFE_IN_SECONDS 3000.0
- (void)sendLocationUpdates
{
// do thing
NSTimeInterval now = [NSDate timeIntervalSinceReferenceDate];
if (now - _startTime > TIMER_LIFE_IN_SECONDS) {
[self.timer invalidate];
self.timer = nil;
}
}
向类中添加实例变量以存储计时器的开始时间:
self.timer = [NSTimer scheduledTimerWithTimeInterval:20 target:self selector:@selector(sendLocationUpdates) userInfo:nil repeats:YES];
_startTime = [NSDate timeIntervalSinceReferenceDate];
YourClass.m:
@interface YourClass () {
NSTimeInterval _startTime;
}
@end
创建计时器时记录当前时间:
self.timer = [NSTimer scheduledTimerWithTimeInterval:20 target:self selector:@selector(sendLocationUpdates) userInfo:nil repeats:YES];
_startTime = [NSDate timeIntervalSinceReferenceDate];
并在sendLocationUpdates
方法中测试当前时间:
#define TIMER_LIFE_IN_SECONDS 3000.0
- (void)sendLocationUpdates
{
// do thing
NSTimeInterval now = [NSDate timeIntervalSinceReferenceDate];
if (now - _startTime > TIMER_LIFE_IN_SECONDS) {
[self.timer invalidate];
self.timer = nil;
}
}
向类中添加实例变量以存储计时器的开始时间:
self.timer = [NSTimer scheduledTimerWithTimeInterval:20 target:self selector:@selector(sendLocationUpdates) userInfo:nil repeats:YES];
_startTime = [NSDate timeIntervalSinceReferenceDate];
YourClass.m:
@interface YourClass () {
NSTimeInterval _startTime;
}
@end
创建计时器时记录当前时间:
self.timer = [NSTimer scheduledTimerWithTimeInterval:20 target:self selector:@selector(sendLocationUpdates) userInfo:nil repeats:YES];
_startTime = [NSDate timeIntervalSinceReferenceDate];
并在sendLocationUpdates
方法中测试当前时间:
#define TIMER_LIFE_IN_SECONDS 3000.0
- (void)sendLocationUpdates
{
// do thing
NSTimeInterval now = [NSDate timeIntervalSinceReferenceDate];
if (now - _startTime > TIMER_LIFE_IN_SECONDS) {
[self.timer invalidate];
self.timer = nil;
}
}
时间间隔20是我发布位置时得到的响应,它是可变的,可以针对每个请求进行更改。当然,您可以动态计算。我更改了发布时间间隔20是我发布位置时得到的响应,它是可变的,可以针对每个请求进行更改。当然,您可以动态计算。我更改了发布时间间隔20是我发布位置时得到的响应,它是可变的,可以针对每个请求进行更改。当然,您可以动态计算。我更改了发布时间间隔20是我发布位置时得到的响应,它是可变的,可以针对每个请求进行更改。当然,您可以动态计算。我更改了post不存储计时器的开始时间。只需安排两个计时器。您的计时器存储在
self.timer
中,第二个计时器将在N小时后启动。当第二个计时器启动时,使您的自拍计时器(第一个)无效。不要存储计时器开始时间。只需安排两个计时器。您的计时器存储在self.timer
中,第二个计时器将在N小时后启动。当第二个计时器启动时,使您的自拍计时器(第一个)无效。不要存储计时器开始时间。只需安排两个计时器。您的计时器存储在self.timer
中,第二个计时器将在N小时后启动。当第二个计时器启动时,使您的自拍计时器(第一个)无效。不要存储计时器开始时间。只需安排两个计时器。您的计时器存储在self.timer
中,第二个计时器将在N小时后启动。当第二个计时器启动时,使您的自我计时器(第一个)无效。这是正确的答案,只是我认为startTime
最好是NSDate
;然后写入if([[NSDate date]timeintervalncedate:startTime]>=计时器(以秒为单位){
@JoshCaswell这会有什么区别呢?我认为这会使类型更清晰,从而使代码更可读。这是正确的答案,只是我认为startTime
最好是NSDate
;然后编写如果([[NSDate date]timeIntervalSinceDate:startTime]>=TIMER\u LIFE\u IN\u SECONDS){
@JoshCaswell这会有什么区别呢?我认为这会使类型更清晰,从而使代码更可读。这是正确的答案,只是我认为startTime
最好是NSDate
;然后编写如果([[NSDate date]timeIntervalSinceDate:startTime]>=TIMER\u LIFE\u IN\u SECONDS){
@JoshCaswell这会有什么区别呢?我认为这会使类型更清晰,从而使代码更可读。这是正确的答案,只是我认为startTime
最好是NSDate
;然后编写如果([[NSDate date]timeIntervalSinceDate:startTime]>=TIMER\u LIFE\u IN\u SECONDS){
@JoshCaswell这会有什么不同?我认为这会使类型更清晰,从而使代码更可读。