NSTimer Restarting - iphone

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.

Related

NSTimer in background

I am very new in Iphone Development. I have an issue . I am using a NSTimer which update a UiLabel in every second. now i have two problem :
when my app goes in background and after it when i open app . app goes hangs.
if i goes next or back on other ui screen then wen i comes on timer screen then my label again shows 0.
can anyone help me.
code which i am using :
timer = [NSTimer scheduledTimerWithTimeInterval: 1.0 target:self selector:#selector(updateCountdown) userInfo:nil repeats: YES];
-(void) updateCountdown
{
secondsLeft--;
//nits testing
if(secondsLeft == 1)
{
[self.view addSubview:recipePage6View.view];
}
if (secondsLeft<0)
{
[timer invalidate];
timer=nil;
lblDisplayTimer.text =#"00:00:00";
}
else
{
hours = secondsLeft / 3600;
minutes = (secondsLeft % 3600) / 60;
seconds = (secondsLeft %3600) % 60;
lblDisplayTimer.text = [NSString stringWithFormat:#"%02d:%02d:%02d", hours, minutes, seconds];
//lblDisplayTimer.text = [NSString stringWithFormat:#"%02d:%02d",minutes,seconds];
}
}
You need to set up a special background timer task and not have an NSTimer on the main run loop which gets suspended in background. The documentation on how to do this is here

iPhone Countdown Timer For a Selected Duration

I wish to create a countdown timer in my app in such a way that I can give a certain duration (like 15 minutes or 15:00) and it starts on an action.
I also need for that countdown timer to take elapsed time duration and total time duration information from an external web service and start the countdown for the remaining time.
This is actually for a quiz application where the user will be presented with a series of questions and the timer will be placed on the question screen (for the whole quiz and not for only one question). The user can set the duration for the quiz and the countdown timer will begin as soon as the user start taking the quiz. There is also a pause functionality where user can pause the test and the information of time elapsed and total time duration will be registered in a web-service. The user can resume the quiz anytime with the countdown timer starting right from the time it was paused (taking this information from the web-service, that is).
Thanks in advance.
PS: To be honest, I had no idea on how to go about it. There was too much of confusion on what format will the time be stored over in the web-service and a lot of other stuff like if the program will go forward after the NSTimer is running in a loop etc. So you know what I mean. Yes, NSTimer does solve the problem as Alex told and provided with a sample code as well.
Look at the docs for NSTimer.
Generally, you would create an NSTimer which calls a method every second, like so:
NSTimer *theTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:#selector(tick) userInfo:nil repeats:YES];
Then in tick, you would decrement a variable holding the time remaining, and update a label on the display.
Also, if you want some working sample code, check out my little countdown app on GitHub.
Try these codes, I have already implemented it in my App. Best for resend OTP type functionalities.
int secondsLeft;
int hours, minutes, seconds;
secondsLeft = 60;
[self countdownTimer];
-(void)countdownTimer {
// secondsLeft = hours = minutes = seconds = 0;
timer = [NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:#selector(updateCounter:) userInfo:nil repeats:YES];
}
- (void)updateCounter:(NSTimer *)theTimer {
if(secondsLeft > 0 ) {
secondsLeft -- ;
hours = secondsLeft / 3600;
minutes = (secondsLeft % 3600) / 60;
seconds = (secondsLeft %3600) % 60;
labelSeconds.text = [NSString stringWithFormat:#"%02d seconds left.",seconds];
} else {
[timer invalidate];
return;
}
}

NSTimer Keep counting after app is closed in iPhone

I have a timer that have to count up to 8 hours (28800 second)
after that it should be released
im wondering how to keep the timer running at the background and/or when application is closed?
this is the NSTimer :
stopWatchTimer = [NSTimer scheduledTimerWithTimeInterval:1.0
target:self
selector:#selector(updateTimer)
userInfo:nil
repeats:YES];
and this is my condition :
counter++;
if (counter >= 28800) {
[stopWatchTimer invalidate];
counter =0;
timeLabel.text = #"Time Out";
}
You can't - once your app is closed then it's not running anymore so the timer won't be running either.
Take a look at local notifications?
When application goes in background, In –(void)applicationDidEnterBackground: application delegate method add current counter value and current time in nsuserdefault.
Now when application becomes active before that –(void)applicationWillEnterForeground: will called so in that method get total seconds application was in background ie (current time of application) - (time when application went background which is stored in nsuserdefault) calculate in seconds
so add this also in –(void)applicationWillEnterForeground :
if((seconds calculated) > (28800 - (current counter value stored in nsuserdefault)))
{
// stop timer as it has gone beyond eight hours
}
else
{
// continue task
}

Increase timeInterval of a Timer [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Change the time interval of a Timer
So I have a timer:
timer=[NSTimer scheduledTimerWithTimeInterval:2 target:self selector:#selector(createNewImage) userInfo:nil repeats:YES];
And I would like that the timer decrease every ten seconds, that scheduledTimerWithTimeInterval goes to 1.5 after ten seconds then to 1.0 after 10 seconds... Is it possible to do this, and if it is possible, how can I do it?
You can't modify a timer after you created it. To change the timer interval, invalidate the old timer and create a new one with the new interval.
You don't have to use several timers, all you have to do is add a variable to use for the time interval, then create a method that invalidates the timer, changes the variable and starts the timer again. For instance you could make a method that starts the timer.
int iTimerInterval = 2;
-(void) mTimerStart {
timer = [NSTimer scheduledTimerWithTimeInterval:iTimerInterval target:self selector:#selector(createNewImage) userInfo:nil repeats:YES];
}
-(void) mTimerStop {
[timer invalidate];
iTimerInterval = iTimerInterval + 5;
[self mTimerStart];
}
This would be the simple way to decrease the timer interval and keep the timer going, but I would personally prefer using the one below, because it makes sure that the timer has only ran once, that way it will not duplicate the instance, forcing your app to become glitchy and it also makes things easier for you.
int iTimerInterval = 2;
int iTimerIncrementAmount = 5;
int iTimerCount;
int iTimerChange = 10; //Variable to change the timer after the amount of time
bool bTimerRunning = FALSE;
-(void) mTimerToggle:(BOOL)bTimerShouldRun {
if (bTimerShouldRun == TRUE) {
if (bTimerRunning == FALSE) {
timer = [NSTimer scheduledTimerWithTimeInterval:iTimerInterval target:self selector:#selector(mCreateNewImage) userInfo:nil repeats:YES];
bTimerRunning = TRUE;
}
} else if (bTimerShouldRun == FALSE) {
if (bTimerRunning == TRUE) {
[timer invalidate];
bTimerRunning = FALSE;
}
}
}
-(void) mCreateNewImage {
//Your Code Goes Here
if (iTimerCount % iTimerChange == 0) { //10 Second Increments
iTimerInterval = iTimerInterval + iTimerIncrementAmount; //Increments by Variable's amount
[self mTimerToggle:FALSE]; //Stops Timer
[self mTimerToggle:TRUE]; //Starts Timer
}
iTimerCount ++;
}
-(void) mYourMethodThatStartsTimer {
[self mTimerToggle:TRUE];
}
I didn't finish all of the coding, but this is most of what you will need. Just change a few things and you'll be good to go!
You will need a set or sequence of timers, one for each different time interval. Stop/invalidate one timer and start the next timer when you want to change to the next time interval in your time sequence.

How to make a UILabel that constantly updates in Objective-C

I'm looking to make a UILabel display the current progress of a timer. Right now to get the current time left I call [timer1 timeLeft]; which returns an int. In this way I can update the label ONCE, at one instant. In what way can I update the label (mainLabel) constantly so that it is always displaying the current timer progress while being somewhat resource efficient?
Thanks for all your help!
Use following code for Countdown timer.
dblElapsedSeconds=0.0; //Declare this in header
tmrElapsedTime = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:#selector(updateElapsedTime) userInfo:nil repeats:YES]; //Declare timer variable in header
-(void)updateElapsedTime
{
dblElapsedSeconds += 1;
//double seconds = [[NSDate date] timeIntervalSinceDate:self.startTime];
int hours,minutes, lseconds;
hours = dblElapsedSeconds / 3600;
minutes = (dblElapsedSeconds - (hours*3600)) / 60;
lseconds = fmod(dblElapsedSeconds, 60);
[lblTimeElapsed setText:[NSString stringWithFormat:#"%02d:%02d",minutes, lseconds]];
}