Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c 目标C:NStimer-如何在n小时后停止计时器_Objective C_Timer_Nstimer - Fatal编程技术网

Objective c 目标C:NStimer-如何在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

我已使用下面的调用启动计时器,需要在n小时后停止

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这会有什么不同?我认为这会使类型更清晰,从而使代码更可读。