NSTimer usage in iOS

Posted By : Vasu Saini | 31-Jan-2018

Today we will discuss how to make timer using NSTimer() 

What is NSTimer? 

The NSTimer class can be utilized to make quick clock protests in your iOS applications. Basically – a quick clock. I'm certain you've all observed iOS applications that have a comparable capacity to this. Ever considered how they function? Thinking about How NSTimer Works? All things considered, it works by sitting tight for a particular interim of time to slip by before it fires. After terminating it will send a predefined message to your quick application target protest. For instance, you can advise your NSTimer question make an impression on a window with directions to play out a refresh after a timeframe has slipped by, or with this illustration refresh a name each second.

Step 1. Create/Open the project in which you want to integrate a timer

Step 2. Take 2 Buttons one to start Timer another to pause it.

Step 3. Make IBActions of both buttons so that we can work on it.

Initialize NSTimer()
Step 4. Define 2 variables on the top of UIViewController class as shown below

var sTimer = NSTimer()
var sCounter = 0

Step 5. Now Take a label on a controller and make outlet of it in class so that we can set a value of the timer in it.

Step 6.Initialize timer's value to label as follows.

override func viewDidLoad() {
        super.viewDidLoad()
        countLabel.text = String(sCounter)
    }

Step 7. Now design IBAction of Start button as follows.

@IBAction func startButton(sender: AnyObject) {
        sTimer = NSTimer.scheduledTimerWithTimeInterval(TIME_INCREMENT, target:self, selector: Selector("FUNCTION"), userInfo: nil, repeats: BOOL)
    }

What this code does is first allocates another occasion of NSTimer() to the Variable SwiftTimer that we started before. Inside this code, there are a couple of things that we have written in capitals; TIME_INCREMENT, FUNCTION, BOOL. We will change these parameters. Here are their main events as shown below.

Parameter Usage
TIME_INCREMENT The number of seconds between firings of the clock. In the event that seconds is not exactly or equivalent to 0.0, this technique picks the nonnegative estimation of 0.1 milliseconds. We will set this to 1.
FUNCTION A function that we will create that will be triggered at the time. We will call this updateCounter.
BOOL If YES, the timer will repeatedly reschedule itself until invalidated. If NO, the timer will be invalidated after it fires. We are going to set this to yes

Step 8. Now we will write actual start function to reflect our usage, as shown below.

@IBAction func startButton(sender: AnyObject) {
    SwiftTimer = NSTimer.scheduledTimerWithTimeInterval(1, target:self, selector: Selector("updateCounter"), userInfo: nil, repeats: true)
}

What actually we have done here is, we told the NSTimer() that we will run this each 1 second. On that second (When the Swift Timer fires) we will run a capacity called updateCounter, and we have likewise instructed it to rehash. 

Presently, we have to make the capacity that will be called when the Swift Timer application runs the NSTimer(). 

Out of the viewDidLoad, make another function called updateCounter. Inside this will advise quickly that we need to get the estimation of sCounter, add one to the esteem and afterwards refresh the label in update counter function as shown below.

func updateCounter() {
    countLabel.text = String(sCounter++)
}

All done with the start functionality now we will know about stop functionality

Step 9. There is a function named as invalidate to stop the function call that function to stop as shown below

@IBAction func pauseButton(sender: AnyObject) {
    sTimer.invalidate()
}

All done.

 

 

About Author

Author Image
Vasu Saini

Vasu Saini is Passionate to deploy ideas into real world, Have zeal to learn new technologies. working as iOS Developer in Oodles Technologies. Sports Freak, Calisthenics ,Parkour, Love to play football, swimmer, athletics etc

Request for Proposal

Name is required

Comment is required

Sending message..