Making Timer (swift) without NSTimer

swift timer not firing
swift 5 timer
countdown timer swift 4
timer invalidate not working swift 4
swift timer background thread
swift dispatch timer
swift timer delay
run timer in background ios swift

Is it possible to make a timer in Xcode 6 without using a NSTimer? By this I mean you can specify a time increment to repeat a certain amount of code? Or to add on is it possible to make a NSTimer that isn't has a selector selecting a different method just continues the code inside the same method the NSTimer is implement in?

It is possible to build a scenario as described in your post. Following code shows the basic idea of what I would do to simulate a timer without NSTimer. Note, by default, the code is using NSThread, alternatively you may set useGCD true to dispatch using GCD.

class Timer: NSObject {

    var interval = 1.0 // Interval at 1.0 second
    var useGCD = false // Set true to use GCD

    var _isTimerRunning = false

    func start() {
        if !_isTimerRunning {
            if !useGCD {
                var thread = NSThread(target: self, selector: Selector("timerFunction"), object: nil)
            } else {
                var queue = dispatch_queue_create("com.example.threading", nil)
                dispatch_async(queue, {
            _isTimerRunning = true

    func stop() {
        _isTimerRunning = false

    func timerFunction() {
        while (_isTimerRunning) {
             * TO-DO Designated code goes here
            NSThread.sleepForTimeInterval(interval) // Interrupt

Start timer:

var timer = Timer()


The ultimate guide to Timer – Hacking with Swift, How to schedule timers, repeat timers, and more. Alternatively, you can do without the timer property by making fireTimer() accept the timer  var timer = NSTimer() //make a timer variable, but don't do anything yet let timeInterval:NSTimeInterval = 10.0 Our next step is declaring a method timerDidEnd. Add this to the code.

Can you use a delay function? Set inside a loop to fire repeatedly.

delay (5.0) {

//code to execute here


func delay(delay:Double, closure:()->()) {
                Int64(delay * Double(NSEC_PER_SEC))
            dispatch_get_main_queue(), closure)

Timers In Swift (How To) – LearnAppMaking, This article explains how to create a timer in Swift. This article shows you how to use the Timer class, formerly known as NSTimer , to schedule timers. When there are no tasks to execute, the run loop waits or quits. Home » Blog » App Development » How To: Working With Timers In Swift. How To: Working With Timers In Swift Written by Reinder de Vries on October 23 2018 in App Development. Timers are super handy in Swift, from creating repeating tasks to scheduling work with a delay. This article explains how to create a timer in Swift.

Sure! I have an objective-c macro I made for this purpose that will likely port to swift in some way or another.

#define startBlockTimer(delayInSeconds, block) {\
__block float runTime = (-1.0f*delayInSeconds);\
__block BOOL keepRunning = YES;\
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0), ^{ for(;keepRunning&&(runTime+=delayInSeconds);[NSThread sleepForTimeInterval:delayInSeconds]) {dispatch_async(dispatch_get_main_queue(), block );}});\
#define blockTimerRunTime runTime
#define stopBlockTimer() keepRunning = NO;

Used like this:

startBlockTimer(0.5, ^{

        self.view.backgroundColor = [UIColor colorWithHue:arc4random_uniform(1000)/1000.0f saturation:0.75f brightness:0.75f alpha:1.0f];

        if (blockTimerRunTime > 5.0f) {


iOS Timer Tutorial, For your first task, you'll create the app's main timer. As mentioned above, Swift's Timer class, otherwise known as NSTimer , is a flexible way to If not, set timer to a repeating Timer that calls  NSTimer. Basically a timer executes some code after a time interval one or more times. With NSTimer you create timer objects. A timer object waits for a specific time interval to elapse and then it fires. When it fires, the timer sends a message to the target object associated with it.

Timer, A timer that fires after a certain time interval has elapsed, sending a specified In iOS 7 and later and macOS 10.9 and later, you can specify a tolerance for a timer class method to create the timer object without scheduling it on a run loop. A timer is not a real-time mechanism. If a timer’s firing time occurs during a long run loop callout or while the run loop is in a mode that isn't monitoring the timer, the timer doesn't fire until the next time the run loop checks the timer. Therefore, the actual time at which a timer fires can be significantly later. See also Timer Tolerance.

Energy Efficiency Guide for iOS Apps: Minimize Timer Use, Some timers are executed without suitable timeouts, causing them to continue firing Create an event stream with the File System Events API. Swift. var myTimer = NSTimer.initWithFireDate(date, interval: 1.0, target: self,  Swift’s Timer class is a flexible way to schedule work to happen in the future, either just once or repeatedly. In this guide I will to provide a selection of ways to work with it, along with solutions for common problems. Note: Before I start, I want to make it clear that there is a significant energy cost to using timers. We’ll look at

Build a count down timer with Swift 3.0, Here you will see how to build a simple timer for iOS using Swift 3.0. You can erase the didReceiveMemoryWarning() function we will not need it in this demo. 5.  NSTimer has a Bool property valid, which indicates the timer is active. If so, we use the invalidate method to shut if off, which also clear memory of the timer. This will clear any active timers, and prevent a possible memory leak. Our current code will need this when we switch manually from run/walk.

  • "Is it possible?" questions always have three potential answers: "Yes, it is possible", "No, it's not possible", and "Maybe, depending on what you mean precisely". Which of those three answers will you accept as correct and helpful? If the answer is "None of the above", then you need to restate your question to be more specific than "Is it possible?", and you should edit to do so. (If one of them is in fact what you want, let me know and I'll post it as an answer instead so you can accept.)