ios - How to put timeout to NSTimer in swift? -


i have nstimer object below :

 var timer = nstimer.scheduledtimerwithtimeinterval(1, target: self, selector: "updatetimer", userinfo: nil, repeats: true) 

i want put timeout timer. maybe know postdelayed method in android. want same thing's swift version. how can ?

nstimer not suited variable interval times. set 1 specified delay time , can't change that. more elegant solution stopping , starting nstimer every time use dispatch_after.

borrowing matt's answer :

// makes playground work 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 {      // delay time     var currentdelay : nstimeinterval = 2      // delayed function     func delaythis() {          // use instead of nstimer         dispatchutils.delay(currentdelay) {             print(nsdate())             // stuffs              // change delay next pass             self.currentdelay += 1              // call function again             self.delaythis()         }     } }  let = alpha()  a.delaythis() 

try in playground. apply different delay each pass of function.


Comments

Popular posts from this blog

sublimetext3 - what keyboard shortcut is to comment/uncomment for this script tag in sublime -

java - No use of nillable="0" in SOAP Webservice -

ubuntu - Laravel 5.2 quickstart guide gives Not Found Error -