'for' loop error with NSTimer - iphone

I want to have an NSTimer that would fire a selector every x seconds if a certain condition (the selector is NO) is true.
The value of x should change like this - 10, 20, 40, 60, 120.
If the selector changes to YES (it returns a BOOL) the timer should stop and change it's initial time to 10 seconds.
I've got this code for a timer:
double i;
for (i= 10.0; i < maxInternetCheckTime; i++) {
[NSTimer scheduledTimerWithTimeInterval:i
target:self
selector:#selector(checkForInternetConnection)
userInfo:nil
repeats:NO];
NSLog(#"Timer is %f seconds", i);
}
But the output I get is just not what I intended to see in the beginning:
2012-12-21 19:25:48.351 Custom Queue[3157:c07] Timer is 10.000000 seconds
2012-12-21 19:25:48.352 Custom Queue[3157:c07] Timer is 11.000000 seconds
2012-12-21 19:25:48.352 Custom Queue[3157:c07] Timer is 12.000000 seconds
2012-12-21 19:25:48.352 Custom Queue[3157:c07] Timer is 13.000000 seconds
2012-12-21 19:25:48.352 Custom Queue[3157:c07] Timer is 14.000000 seconds
2012-12-21 19:25:48.352 Custom Queue[3157:c07] Timer is 15.000000 seconds
And so on.
What am I doing wrong in this pretty trivial task?

for (i= 10.0; i < maxInternetCheckTime; i++) {
[NSTimer scheduledTimerWithTimeInterval:i
You are scheduling a set of 10 timers at the same moment to be executed after: 10, 11, 12,13, etc seconds.
You need just one timer to start with:
[NSTimer scheduledTimerWithTimeInterval:10
target:self
selector:#selector(checkForInternetConnection:)
userInfo:nil
repeats:NO];
then in checkForInternetConnection you schedule a new one if needed:
-(void)checkForInternetConnection:(NSTimer*)firedTimer {
float interval = firedTimer.timeInterval;
interval *= 2;
if (<CONDITION>) {
[NSTimer scheduledTimerWithTimeInterval:interval
target:self
selector:#selector(checkForInternetConnection)
userInfo:nil
repeats:NO];
}
}
I hope the logic is clear:
you schedule a check;
you do the check;
if check is not ok, you schedule a new one.
Hope it helps.

You are printing i, which is increased by 1 in every cycle beginning from 10. This is the correct output.

Related

I want schedule timer with repeat count

I want to invoke a method 24 times but between each invocation I want an interval of 1 sec,
now am using
[NSTimer scheduledTimerWithTimeInterval:1 target:self selector:#selector(loadNumber) userInfo:nil repeats:YES];
but how to count invocations, I want to invalidate timer after 24 count can I invalidate that.
Create an instance variable for an int, it doesn't matter what you call it. Set it to "0" before you call the timer.
someInt = 0;
[NSTimer scheduledTimerWithTimeInterval:1 target:self selector:#selector(loadNumber:) userInfo:nil repeats:YES];
In the action that the time calls check the value of this number to make sure the action should be performed and increment the number.
- (void)loadNumber:(NSTimer *)sender
{
if (someInt <= 24) {
//do something
someInt ++;
}else{
[sender invalidate];
someInt = 0;
}
}
You should set up a counter in loadNumber, which will invalidate your NSTimer after your desired repeat counts.

slider increment automatically in time period

I am trying to use the current time in my movie player to increment a sliders value in my iPad app.
slider.maximumValue=10;
slider.minimumValue=0;
How can I increment slider.value from 0 to 10 in a specific time period?
You can use an NSTimer to repeatedly add 1 to your slider value if you know the specific time period beforehand:
NSTimer *aTimer = [NSTimer scheduledTimerWithTimeInterval:(timePeriod/slider.maximumValue)
target:self selector:#selector(timerFired:) userInfo:nil repeats:YES];
- (void)timerFired:(NSTimer*)theTimer {
[slider setValue:slider.value + 1.0];
if(slider.value == slider.maximumValue) {
[theTimer invalidate];
}
}

Calling function with interval but until some time passed Iphone

I need to make a call every .025 seconds over a duration of 30 seconds. Here is my .025 second timer.
NSTimer* myTimer = [NSTimer scheduledTimerWithTimeInterval: 0.025 target: self
selector: #selector(GetScreen:) userInfo: nil repeats:YES ];
How can I restrict this to a duration of 30 seconds?
If you only want it to continue for 30 seconds then you would just make a member variable to increment until it hits 30 seconds then you turn off your timer:
CGFloat timerTotal_;
//initialize it where you create your timer
timerTotal_ = 0.0f;
//every time your GetScreen: is called
timerTotal_+= .025;
if(timerTotal_ > 30)
{
[myTimer invalidate];
}
If you are trying to wait until some condition has been met, don't use a timer to do that. Use a delegate pattern, or notification/observer or semaphore, etc.
If you want to call GetScreen: (which should probably be getScreen: by normal Obj-C convention) once, after 30s, use this:
[ NSTimer scheduledTimerWithTimeInterval:30.0 target:self selector:#selector( getScreen: ) userInfo:nil repeats:NO ] ;

How to call a countdown timer from 5 second and it should decrement to 1 second in iPhone?

I have an application in which I want that when I run my application initially it should show a screen for 2 seconds that displays warning about the app and just after 2 seconds a countdown should start from 5 seconds that should decrement from 5 second to 1 second.
There are two ways:
[NSTimer timerWithTimeInterval: .....]
[self performSelector: withObject: afterDelay:]
so if i got your point you want to display a warning screen and after 2 seconds you want to show the user count down start from 5 and end at 1 .. well this can done easy by using a timer and counter as following :
define a NSTimer and start it once the warning view is shown .. your definition will be like this:
timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:#selector(timerFires) userInfo:nil repeats:YES];
[timer fire];
define a global integer and set its initial Value to 7 (lets suppose you name it counter).
-in timerFires selector you decreament counter by 1 and check its value when its equal to 5 start to show its vlaue on UILabel for example and when its 1 invalidate the timer and do you what you want at this point .. the timerFires will be like this:
- (void)timerFires
{
counter --;
if(counter ==5)
{
//show its Value
}
if(counter ==1)
{
[timer invalidate];
timer = nil;
//Do other stuff
}
}
[NSTimer scheduledTimerWithTimeInterval:60 target:self selector:#selector(updateFunction) userInfo:NULL repeats:YES];
In updateFunction you can write your code for count down.
You can also set "repeats:NO" and to call the updateFunction at the end of itself if it is necessary

Slider with NSTimer iPhone

I want to display an image for 5-6 secs and then link a slider to it to show increasing time. That is say if time is 0 seconds, slider is at extreme left and if Timer is 1 seconds slider moves appropriately towards right and if Timer is 3 seconds than slider is in the middle and as time increases gradually 4 -5 seconds it moves right and at 6 seconds its on its extreme right. Can you guide me on this
Use below one
timerForSlider = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:#selector(updateSlider) userInfo:nil repeats:YES];//timerForSlider in .h file
-(void)updateSlider
{
[slider setValue:slider.value+1];
if(slider.value==6)
{
[timerForSlider invalidate];
}
}
In XIB set slider minimumVal =0; MaximumVal=6; initialVal = 0;
this is timer callback (selector):
-(void) timer {
//dSliderValue - step of slider moveing. set it to 2
if (slider.value+dSliderValue <= 60) [slider setValue:slider.value+dSliderValue];
else {
[t invalidate];
//any other code here, on timer stopping
}
}
this is timer (NSTimer *t). call it on start image showing:
[slider setMaximumValue:60];
[slider setValue:0];
dSliderValue = 2;
t = [NSTimer scheduledTimerWithTimeInterval:0.2 target:self selector:#selector(timer) userInfo:nil repeats:YES];