Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to automatic call function every x time [duplicate]

Possible Duplicate:
How to call function in every “X” minute?

How to periodically call a certain function?

- (void)viewDidLoad
{ 
    [super viewDidLoad];
    [self checkAlert];   
}



-(void)checkAlert{
    // Server output Alert Note
    NSString *alert_note_hostStr = @"http://www.loxleyintranet.com/MobileApplication/xml/alert_note.php";
    NSData *alert_note_dataURL = [NSData dataWithContentsOfURL:[NSURL URLWithString:alert_note_hostStr]];
    NSString *alert_note_serverOutput = [[NSString alloc] initWithData:alert_note_dataURL encoding:NSASCIIStringEncoding];

    if ([alert_note_serverOutput isEqualToString:@"0"]) {
        alertImage.hidden = YES;
        alertLabel.hidden = YES;
        underMainBG.hidden = YES;
        alertLabel.text = [NSString stringWithFormat:@"No Alert"];
    }else{    
        alertLabel.text = [NSString stringWithFormat:@"You've Got Note (%@)" ,alert_note_serverOutput];

    }    
}

How can I call [self checkAlert]; every x minutes or seconds?

like image 717
Vasuta Avatar asked May 15 '12 09:05

Vasuta


2 Answers

Use [NSTimer scheduledTimerWithTimeInterval:60.0 target:self selector:@selector(checkAlert) userInfo:nil repeats:YES];.

like image 162
jimpic Avatar answered Sep 30 '22 15:09

jimpic


[NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(myTimerTick:) userInfo:nil repeats:YES]; // the interval is in seconds...

...then your myTimerTick: method should look like this..

-(void)myTimerTick:(NSTimer *)timer
{
    if(some_contiditon)
    {
        // Do stuff...
    }
    else
    {
        [timer invalidate]; //to stop and invalidate the timer.
    }
}
like image 37
graver Avatar answered Sep 30 '22 14:09

graver