2012-05-15 19 views
11

Duplicar posible:
How to call function in every “X” minute?función de llamada automática cada vez que x

Cómo llamar periódicamente una determinada función?

- (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]; 

    }  
} 

¿Cómo puedo llamar [self checkAlert]; cada x minutos o segundos?

Respuesta

25

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

+0

Oh .... wow ... es el trabajo ... gracias verymuch jimpic – Vasuta

+0

No trabajo para mí .. – shim

+0

make repeat = NO, en lugar de tener un mejor resultado. Gracias –

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

... entonces su método myTimerTick: debería tener este aspecto ..

-(void)myTimerTick:(NSTimer *)timer 
{ 
    if(some_contiditon) 
    { 
     // Do stuff... 
    } 
    else 
    { 
     [timer invalidate]; //to stop and invalidate the timer. 
    } 
} 
+0

oh gracias muy – Vasuta

+1

Esto funciona. Aún en 2016. – Paul

Cuestiones relacionadas