如果找到正确的结果,你如何杀死重复的计时器

时间:2011-12-25 07:51:11

标签: ios xcode webview nstimer

我在Web视图加载中有一个NS Timer Firing,用于在页面上查找信息。然后,我对页面设置复选框等进行了一些操作,这些操作带来了一些AJAX函数,人们可能知道这些函数不会触发webviewloaded方法。简而言之,我想创建一个每5秒检查一次的时间来检查只有在加载正确信息时才会出现的特定部分。这很好,我可以这样做,但我不知道如何停止NSTIMER,因为这里的所有示例都没有显示传递给函数的参数。

目前我有

   [NSTimer scheduledTimerWithTimeInterval:5 target:self selector:@selector(fcheckForEditFinished) userInfo:nil repeats:YES];

-(void) fcheckForEditFinished
{
//Would like to kill timer at this point. 
}

谢谢Kevin

1 个答案:

答案 0 :(得分:2)

documentation of scheduledTimerWithTimeInterval:target:selector:userInfo:repeats:说:

  

aSelector
  定时器触发时发送到目标的消息。该   选择器必须具有以下签名:
  - (void)timerFireMethod:(NSTimer*)theTimer
  计时器将自身作为此方法的参数传递。

你可以通过发送invalidate消息来“杀死”一个计时器。因此,将fcheckForEditFinished更改为fcheckForEditFinished:,如下所示:

- (void)fcheckForEditFinished:(NSTimer *)timer
{
    if (checkForEdit()) {
        [timer invalidate];
    }
}

计时器将自动传递为fcheckForEditFinished:的参数。确保在调用scheduledTimerWithTimeInterval:...时将冒号添加到选择器。