Reputation: 1760
I want my cycle to execute each iteration with an interval in one (two/n) second. How can I do it? I tried to use sleep (5)
but i think it is wrong decision.
I thought about timer^ but i thing it's wrong idea too
self.syncTimer = [NSTimer scheduledTimerWithTimeInterval:5.0f
target:self
selector:@selector(serverSync)
userInfo:nil
repeats:YES];
and selector
-(void) serverSync {
NSLog (@"Hello");
}
In that case i will have Hello every 5 seconds.
And i need
for (int i = 0; i < Array.count; i ++) {
NSLog (@"Hello - %d", i);
some code;
}
And it must look as
Upvotes: 1
Views: 125
Reputation: 4311
Also you could use something like this (but it not so clear as using NSTimer)
- (void)sayHello {
__block NSInteger iteration = 0;
[self execute:^{
NSLog(@"Hello: %d", iteration);
iteration++;
}
withCount:5
delay:0.2];
}
- (void)execute:(void(^)(void))executor repeatCount:(NSInteger)count delay:(NSTimeInterval)delayInSeconds{
if (count == 0) {
return;
};
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
dispatch_after(popTime, dispatch_get_main_queue(), ^{
executor();
[self execute:executor withCount:count - 1 delay:delayInSeconds];
});
}
Upvotes: 0
Reputation: 1129
You could use something like this:
int count;
self.syncTimer = [NSTimer scheduledTimerWithTimeInterval:5.0f
target:self
selector:@selector(nextHello)
userInfo:nil
repeats:YES];
-(void)nextHello {
if (count < 999) {
NSLog (@"Hello - %d", count);
some code;
count++;
} else {
[self.syncTimer invalidate]; //Stop Timer
}
}
Or if you don't want to use NSTimer you can use performSelector
afterDelay
. Like this:
[self performSelector:@selector(nextHello:) withObject:[NSNumber numberWithInt:0]; //Start the Cycle somewhere
-(void)nextHello:(NSNumber*)count {
NSLog (@"Hello - %@", count);
Some Code
[self performSelector:@selector(nextHello:) withObject:[NSNumber numberWithInt:[count intValue]+1] afterDelay:5.0];
}
Upvotes: 1
Reputation: 2921
This is how you delay time in C:
#include <STDIO.H>
#include <TIME.H>
int main()
{
int i;
for(i = 10; i >= 0; i--)
{
printf("%i\n",i); // Write the current 'countdown' number
sleep(1); // Wait a second
}
return 0;
}
Upvotes: 0