I have a For Loop in which I am trying to run 52 times. I want it to run once, pause a few seconds, run the second time, pause a few seconds, etc. I know that sleep() isn't good to use, but I can't seem to get NSTimer to work since it requires a method to execute after it is out of time. Is there anyway to make NSTimer pause the execution and then execute the local for loop rather than a function? Is there a different way to pause during each execution in the for loop? Thanks!
I have a For Loop in which I am trying to run 52 times. I want it to run once, pause a few seconds, run the second time, pause a few seconds, etc. I know that sleep() isn't good to use, but I can't seem to get NSTimer to work since it requires a method to execute after it is out of time. Is there anyway to make NSTimer pause the execution and then execute the local for loop rather than a function? Is there a different way to pause during each execution in the for loop? Thanks!
NSTimer is definitely what you want - what's wrong with it? instead of for(int i = 0;i<someNumber; i++) , you'll want to put the variable "i" at the class level (and give it a meaningful name, like "frameCount").
When you start the timer, set frameCount to 0.
When the timer fires, have it call a method that (1) performs your task (2) adds one to frameCount (3) checks if frameCount>someNumber; if so, invalidate the timer and call another method for whatever happens after the loop.
Don't use a long loop to cause a delay - that will keep your main thread from performing other tasks, like responding to touches, phone calls, and other events.
__________________
Free Games!
Last edited by smasher; 02-21-2009 at 01:22 PM.
Reason: typos
When the timer fires, have it call a method that (1) performs your task (2) adds one to frameCount (3) checks if frameCount>someNumber; if so, invalidate the timer and call another method for whatever happens after the loop.
I'm not sure I follow you... I want a for loop that pauses for like 3 seconds at the end of each execution before it starts for the next iteration. Is the following like what you are describing?
It's not clear to me if you:
(A) want to do something 52 times, pausing for 3 seconds after EACH SOMETHING (total time, 52x3 seconds)
(B) want to do something 52 times, pause for 3 seconds, then repeat (B) (total time: who knows.)
I'm gonna go with (A) because it's the harder question.
declare "int frameCount" and "NSTimer *pauseTimer" in your .h file.
then:
Code:
// this was almost OK -
//I just removed the local declaration of pauseTimer,
//took the colon off of myFunction (because it takes no params) and set repeats:YES
- (void)startTimer {
frameCount = 0;
pauseTimer = [NSTimer scheduledTimerWithTimeInterval:3.0 target:self selector:@selector(myFunction) userInfo:nil repeats:YES];
}
- (void)myFunction {
//Do your thing here -
//whatever you want to happen every 3 seconds.
NSLog(@"Doing My thing.");
//count by one, and check if we're done.
frameCount++;
if (frameCount >= 52)
[pauseTimer invalidate];
}
If you wanted (B) instead , just add your loop inside myFunction, and remove or adjust the "if" statement.
Hello, I have an if statement that I would like to add a loop with delay in it, But I cant seem to figure it out.
I have something like this.
Code:
if (myCount == 2) {
NSLog(@"TAP 2");
// Im playing a sound here. which plays fine.
// I would like to add a timer or some sort of delay to fire the sound to play again after a set amount of time.
}
I'm not sure If I should use an NStimer or a for loop, or something else.
Hello, I have an if statement that I would like to add a loop with delay in it, But I cant seem to figure it out.
{snip}
I'm not sure If I should use an NStimer or a for loop, or something else.
You shouldn't use a "for" loop or "sleep" to pause the main thread - that'll keep input events from being processed, etc. Use an NSTimer instead. The example code I posted should do it. If you only need it to repeat once then you don't need the variable frameCount, and you can set repeats:NO instead.
You shouldn't use a "for" loop or "sleep" to pause the main thread - that'll keep input events from being processed, etc. Use an NSTimer instead. The example code I posted should do it. If you only need it to repeat once then you don't need the variable frameCount, and you can set repeats:NO instead.
Smasher, thanks for the quick reply, I guess where my hang up is that I have something like this.
Code:
- (IBAction)pushButton1 {
if (isLooping == YES){
// Stop looping sound
isLooping = NO;
NSLog(@"Looping stopped");
}
else if (myCount == 1) {
NSLog(@"TAP 1");
//play sound once
}
else if (myCount == 2) {
NSLog(@"TAP 2");
//play sound once
// I would like to add a timer or some sort of delay to fire the
// sound to play again after a set amount of time.
isLooping = YES;
}
}
So the NSTimer calls a function, then how do I add the function inside my else if (myCount == 2) statment?
Would I do something like this?
Code:
- (void)startTimer {
pauseTimer = [NSTimer scheduledTimerWithTimeInterval:3.0 target:self selector:@selector(myFunction) userInfo:nil repeats:YES];
}
- (IBAction)pushButton1 {
if (isLooping == YES){
// Stop looping sound
isLooping = NO;
NSLog(@"Looping stopped");
}
else if (myCount == 1) {
NSLog(@"TAP 1");
//play sound once
}
else if (myCount == 2) {
NSLog(@"TAP 2");
- (void)myFunction {
//play sound once
//this will happen every 3 seconds.
NSLog(@"Doing My thing.");
[pauseTimer invalidate];
}
isLooping = YES;
}
}
Also I want the timer to start when the else if (myCount == 2) statment is called, not when the app starts if that makes sense.
The line with "scheduledTimerWithTimeInterval" is the one that starts the timer, so either that line should go inside the if (myCount == 2) statement or you should put [self startTimer] inside the if statement.
The method "myFunction" should go *outside* the pushButton1 method. You don't write one method inside another (although you can, of course, *call* one method from inside another.)
The line with "scheduledTimerWithTimeInterval" is the one that starts the timer, so either that line should go inside the if (myCount == 2) statement or you should put [self startTimer] inside the if statement.
The method "myFunction" should go *outside* the pushButton1 method. You don't write one method inside another (although you can, of course, *call* one method from inside another.)
Thanks Smasher!
I got it all worked out now, the only thing that I'm faced with now is that, the sounds are in a Scroll View, and once the user hits the button the the sound is played, a "for" loop is fired to play a small animation, then the timer starts and after a set period of time it calls the method "myFunction" which then plays the sound again and the small animation. It all works how I want it to except when I start to scroll, it seems to be pausing the timer and then when I stop scrolling the method "myFunction" is fired. This is a pretty big issue.
I know its not how im playing the sound or the animation, because when I set that to just loop via the sound manager the sound and animation play fine while scrolling.
UIScrollView pauses NSTimer until scrolling finishes
So I found a little info about my issue with UIScrollView pauses NSTimer until scrolling finishes, but im not sure that the " NSRunLoop method:" is or how to use it.
"this is a run loop issue. Specifically, you need to make use of the NSRunLoop method:"