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 ] ;
Related
I have CountDown timer like below:
- (void)updateCounterLabel:(NSTimer *)theTimer {
if(secondsLeft > 0 ){
secondsLeft -- ;
hours = secondsLeft / 3600;
minutes = (secondsLeft % 3600) / 60;
seconds = (secondsLeft %3600) % 60;
countDownLabel.text = [NSString stringWithFormat:#"%02d:%02d:%02d", hours, minutes, seconds];
}
else{
secondsLeft = timeInterval;
}
-(void)countdownTimer {
if([timer isValid]){
[timer invalidate];
timer = nil;
}
timer = [NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:#selector(updateCounterLabel:) userInfo:nil repeats:YES];
}
----My problem is that every time recall this timer it increments like this : secondLeft-3 , secondLeft -5 ,secondLeft - 7..................
Each time my view loads, i create a new timer, but the old ones still exist.in my timer's action method, i am decrementing an index variable that keeps track of the seconds, and each timer runs that method every time it fires. So, if i have three timers, the index will decrements by three each time.
For example:
First Load: 60, 59, 58...
Second Load: 60, 58, 56...
Third Load: 60, 57, 54...
Question : How can i restart or recreate a timer without above problem? somebody help me out pls.
The
timer = [NSTimer scheduledTimerWithTimeInterval:...];
call retains the target (the view controller) while the timer is running,
so that the view controller
is never deallocated and the timer continues to run even if the view controller
is popped off the navigation stack or dismissed.
Therefore, if the view is loaded again, you have two instances of the view controller and
therefore two timers, which both decrement the same global variable secondsLeft.
This hopefully explains why the value is decremented by two each second.
As a solution, you can create the timer in viewWillAppear, and invalidate it
in viewWillDisappear.
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.
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.
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
I need to show a timer counting down from 30 to 0, several times. (30 to 0, start over at 30, etc) However when I placed it in a for-loop, instead of waiting till the timer invalidates to begin the next timer, the loop iterates through and several timers are created.
How can I form a loop that waits until the timer has invalidated?
- (IBAction)startCountdown:(id)sender {
NSNumber *rounds = [[NSNumber alloc]initWithInt:sliderRounds.value];
for (int i = rounds.intValue; i > 0; i--) {
[self timer];
}
You can use the scheduledTimerWithTimeInterval
[NSTimer scheduledTimerWithTimeInterval: 0.5
target: self
selector: #selector(handleTimer:)
userInfo: nil
repeats: NO];
And inside the handleTimer you create the next timer
Looks like you are calling some method called 'timer' on self. What does it do? Creates a new timer? Then this is why. When a timer is created, it doesn't block. Instead a method call is scheduled and executed once the timer timeout passes.
What you could do is the following:
Create a class that holds an NSTimer
This class would have a variable to hold how many times it needs to count
At the end of one timer (and really, the only one) firing, it decrements the count
You can have the NSTimer repeat by calling it with:
[NSTimer scheduledTimerWithTimeInterval:30.0f
target:self
selector:#selector(timerFired)
userInfo:nil
repeats:YES];
In a method called - (void)timerFired, you would decrement the count. If it reaches zero, you would stop the timer.
Hope this helps!