0

I have a NSTimer object as below :

 var timer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: "updateTimer", userInfo: nil, repeats: true)

I want to put timeout to my timer. Maybe you know postdelayed method in android. I want to same thing's swift version. How can I do this ?

sorunluadam
  • 3,157
  • 3
  • 18
  • 30
  • what should the timeout do? for those of use who don't know postdelayed. After x time the timer should stop? – R Menke Feb 05 '16 at 13:56
  • isn't that the definition of a timer? – Russell Feb 05 '16 at 13:57
  • I want to delay timer 3 second . then It can work again – sorunluadam Feb 05 '16 at 13:58
  • @Russell `NSTimer` fires a function after x time and may or may not repeat. A timeout to me sounds like a way to stop it after y time. (this is vauge I know) – R Menke Feb 05 '16 at 13:58
  • So you want to delay a delay? Because in essence an `NSTimer` is a delay – R Menke Feb 05 '16 at 13:59
  • 1
    maybe we should all wait for @yusufonder to actually explain what the question is :-) – Russell Feb 05 '16 at 14:03
  • I understand what you are saying but I can't tell my problem :) Maybe I should study english :) Anyway . My goal is this : when timer is working. I want to cancel my timer . Then timer should re-work 10 second later. But this event should be automatically . – sorunluadam Feb 05 '16 at 14:08
  • So timer should not work spesific time . Then it should work again – sorunluadam Feb 05 '16 at 14:12
  • So you want to delay a certain function for a different amount of time each time it happens? Take a look at this [answer](http://stackoverflow.com/a/24318861/4263818). It is more useful for what you want to achieve – R Menke Feb 05 '16 at 14:12
  • yes exacly. I am looking this question then I will return to you – sorunluadam Feb 05 '16 at 14:19

1 Answers1

6

NSTimer is not suited for variable interval times. You set it up with one specified delay time and you can't change that. A more elegant solution than stopping and starting an NSTimer every time is to use dispatch_after.

Borrowing from Matt's answer :

// this makes a playground work with GCD
XCPlaygroundPage.currentPage.needsIndefiniteExecution = true

struct DispatchUtils {

    static func delay(delay:Double, closure:()->()) {
        dispatch_after(
            dispatch_time(
                DISPATCH_TIME_NOW,
                Int64(delay * Double(NSEC_PER_SEC))
            ),
            dispatch_get_main_queue(), closure)
    }
}


class Alpha {

    // some delay time
    var currentDelay : NSTimeInterval = 2

    // a delayed function
    func delayThis() {

        // use this instead of NSTimer
        DispatchUtils.delay(currentDelay) {
            print(NSDate())
            // do stuffs

            // change delay for the next pass
            self.currentDelay += 1

            // call function again
            self.delayThis()
        }
    }
}

let a = Alpha()

a.delayThis()

Try it in a playground. It will apply a different delay to each pass of the function.

Community
  • 1
  • 1
R Menke
  • 7,577
  • 4
  • 32
  • 59