Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7404555
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T05:16:18+00:00 2026-05-29T05:16:18+00:00

I am using NSTimer to make a stopwatch. I would like it to continue

  • 0

I am using NSTimer to make a stopwatch. I would like it to continue working if the user switches to a different app, but right now it only works when the app is running. I figure that this is probably pretty simple to fix by recording a timestamp when the timer starts, but I’m not really sure how to reconcile that with the ability to pause/restart the timer in the middle.

My code:

-(IBAction)start;
{
    if(sharedInstance.timer == nil){
    sharedInstance.timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self     selector:@selector(showActivity) userInfo:nil repeats:YES];
    sharedInstance.timerRunning = YES;
    }
}


-(IBAction)stop;
{
    [sharedInstance.timer invalidate];
    sharedInstance.timer = nil;
    sharedInstance.timerRunning = NO;
}


-(void)showActivity;
{
    sharedInstance.elapsedTime += 1;

    int hours = sharedInstance.elapsedTime / 3600 ;
    int minutes = sharedInstance.elapsedTime / 60 - hours * 60; 
    int seconds = sharedInstance.elapsedTime - hours * 3600 - minutes * 60;

    sharedInstance.timeString = [NSString stringWithFormat:@"%02d:%02d:%02d", hours, minutes, seconds];
    self.readout.text = sharedInstance.timeString;
}

-(void)viewDidLoad {
    sharedInstance = [DataSingleton sharedInstance];
    if (sharedInstance.timeString==nil) {
    sharedInstance.timeString=@"00:00:00";
    }
    self.readout.text = sharedInstance.timeString;
    [sharedInstance.timer invalidate];
    sharedInstance.timer = nil;
    if(sharedInstance.timerRunning) {
        [self start];
    }
    [super viewDidLoad];
}

-(void)reset {
    [self stop];
    sharedInstance.elapsedTime = 0;
    sharedInstance.timeString = @"00:00:00";
    self.readout.text = sharedInstance.timeString;
} 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T05:16:19+00:00Added an answer on May 29, 2026 at 5:16 am

    Every time you call showActivity you could get a new time stamp and subtract the old one from it getting the change in time. Then add that to elapsedTime instead of 1.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've been using an NSTimer successfully, but am now having trouble with it. Undoubtably
I'm using an NSTimer to do some rendering in an OpenGL based iPhone app.
I am using an NSTimer which I have working to show minutes and seconds.
I am using NSThread along with NSTimer. My code is like this -(void) checkForRecentAlarm
I recently switched from using NSTimer to CVDisplayLink to redraw my OpenGL animation, but
In my app, I'm using NSTimer and using initWithFireDate. I want to fire the
I am trying to create a slide show using NSTimer... But the following code
Using VS2008, C#, .Net 2 and Winforms how can I make a regular Button
Using C#, I need a class called User that has a username, password, active
I am porting an app that I originally wrote using the accelerometer for IOS

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.