Objective C equivalent to javascripts setTimeout?

后端 未结 4 1026
没有蜡笔的小新
没有蜡笔的小新 2020-12-30 21:10

I was wondering whether there is a solution to raise an event once after 30 seconds or every 30 seconds in CocoaTouch ObjectiveC.

4条回答
  •  一向
    一向 (楼主)
    2020-12-30 21:50

    Take a look at the NSTimer class:

    NSTimer *timer;
    ...
    timer = [[NSTimer scheduledTimerWithTimeInterval:30.0 target:self selector:@selector(thisMethodGetsFiredOnceEveryThirtySeconds:) userInfo:nil repeats:YES] retain];
    [timer fire];
    

    Somewhere else you have the actual method that handles the event:

    - (void) thisMethodGetsFiredOnceEveryThirtySeconds:(id)sender {
       NSLog(@"fired!");
    }
    

提交回复
热议问题