iPhone Timer - How to wait seconds?
I'm trying to wait seconds ..
for (int i = 0; i < 3; i++) {
// something A
...
// wating
[NSTimer scheduledTimerWithTimeInterval:10
target:self
selector:@selector(endTimer:)
userInfo:nil
repeats:YES];
// something B
...
}
- (void)endTimer:(NSTimer *)timer {
NSLog(@"end timer");
[timer invalidate];
}
I wanna 'A -> waiting -> B'
but don't wait ...
A -> B -> A -> B -> A -> B -> end timer, end timer, end timer
Is there 开发者_运维知识库any way?
good day
try this,
-(void)callA{
//Do your thing...
}
-(void)callB{
//Do your thing...
}
-(void)callFunction{
count++;
if(count<3){
[self performSelector:@select(callA) withObject:nil];
[NSThread sleepForTimeInterval:3.0];
[self callB];
}
else{
[timer invalidate];
}
}
Now, create timer in main function from which you want to call the above function.
timer = [NSTimer scheduledTimerWithTimeInterval:3.0 target:self selector:@selector(callFunction) userInfo:nil repeats:YES];
Even if your question is not phrased well,
// set myCount somewhere as an INT with value 0
// int myCount = 0;
-(void)callA
{
if (myCount < 3){
[self performSelector:@selector(callB) withObject:nil afterDelay:1.0];
}
}
-(void)callB
{
myCount +=1;
[self performSelector:@selector(callA) withObject:nil afterDelay:1.0];
}
精彩评论