I need to start NSTimer by 0:0:0 that I am doing and I have a Pause button on click of that the timer has to pause after some time if a user clicks play it has to run from paused value of time. How can I pause and play in NSTimer? Any help? Thanks in advance.
-
1Are you just trying to keep track of some amount of elapsed time? NSTimer isn't really for that purpose. It's purpose is more-or-less to schedule events to fire within the run loop. – jmstone617 Apr 02 '12 at 11:32
-
possible duplicate of [Stopwatch using NSTimer incorrectly includes paused time in display](http://stackoverflow.com/questions/8101231/stopwatch-using-nstimer-incorrectly-includes-paused-time-in-display) – jscs Apr 03 '12 at 01:42
6 Answers
Try this shizzle... worked great for me
EDIT
To be honest my actual code is this:
-(IBAction)clicked:(id)sender
{
if ([startStop.titleLabel.text isEqualToString:@"Start"])
{
[startStop setTitle:@"Pause" forState:UIControlStateNormal];
[startStop setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(countUp) userInfo:nil repeats:YES];
timerDown = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(countDown) userInfo:nil repeats:YES];
} else if ([startStop.titleLabel.text isEqualToString:@"Pause"])
{
[startStop setTitle:@"Resume" forState:UIControlStateNormal];
[startStop setTitleColor:[UIColor colorWithRed:0/255 green:0/255 blue:255/255 alpha:1.0] forState:UIControlStateNormal];
pauseStart = [[NSDate dateWithTimeIntervalSinceNow:0] retain];
previousFireDate = [[timer fireDate] retain];
[timer setFireDate:[NSDate distantFuture]];
} else if ([startStop.titleLabel.text isEqualToString:@"Resume"])
{
[startStop setTitle:@"Pause" forState:UIControlStateNormal];
[startStop setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
float pauseTime = -1*[pauseStart timeIntervalSinceNow];
[timer setFireDate:[NSDate dateWithTimeInterval:pauseTime sinceDate:previousFireDate]];
[pauseStart release];
[previousFireDate release];
}
}
I used a UIButton and used this code when clicked.
remember to put this:
NSDate *pauseStart, *previousFireDate;
in your .h file
EDIT!
here is the countUp method. forget the down method. seconds, minutes, hours are Ints
- (void)countUp
{
seconds += 1;
if (seconds == 60)
{
seconds = 0;
minutes++;
if (minutes == 60)
{
minutes = 0;
hours++;
}
}
}

- 1,377
- 1
- 11
- 17
-
how did u passed NSTimer parameter to those 2 methods? b'coz i have paused seconds in NSString,,, – taus-iDeveloper Apr 02 '12 at 13:51
-
thanks for the update, i think u forgot to include countUp & countDown method code, can u plz let me know what is there inside those methods? – taus-iDeveloper Apr 02 '12 at 14:34
You don’t. Instead of pausing simply invalidate and release the old timer and when the users presses Play again, create a fresh timer.

- 102,279
- 44
- 260
- 354
-
thanks for the reply,how to create fresh timer?i mean to which NSTimer method i need to pass my PausedTime and how b'coz i'm having it in NSString format. – taus-iDeveloper Apr 02 '12 at 13:48
refreshTimer
should be on class level. Use the following code to stop the timer:
refreshTimer = [NSTimer scheduledTimerWithTimeInterval: refreshInSeconds
target: self
selector: @selector(refreshPage)
userInfo: nil
repeats: YES];
[refreshTimer invalidate];
refreshTimer = nil;
To start you can again schedule timer as Line 3
Use following code to start timer with arguments also:
timer = [NSTimer scheduledTimerWithTimeInterval:time target:self selector:@selector(callMyMethod:) userInfo:[NSDictionary dictionaryWithObjectsAndKeys:web1, @"web", baseURL, @"baseURL", option1, @"option", nil] repeats:NO];
and then use
[timer invalidate];
to stop the timer.
I hope this code will help you.
Thanks.

- 1,224
- 9
- 15
there is no such thing as pause for NSTimer, you have to stop and start it again.
[yourTimer invalidate]; //will stop the timer after which you can start it again

- 135
- 5
Updated answer for Swift 2
import UIKit
class ViewController: UIViewController {
var myTimer = NSTimer()
var time = 0 // global var
@IBOutlet weak var counterLabel: UILabel!
@IBAction func pauseTimer(sender: AnyObject) {
myTimer.invalidate()
}
@IBAction func startTimer(sender: AnyObject) {
myTimer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: Selector("result"), userInfo: nil, repeats: true)
}
func result() {
time++
counterLabel.text = String(time)
print(time)
}
override func viewDidLoad() {
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}

- 1,521
- 1
- 13
- 12