How to loop NSTimer in SWIFT with a specific number of counting -


i trying loop timer specific number of counting... eg... 1, 2, 3, 4, 5

then loop again, same counting, 10 times, stop.

i able counting, can't looping of counting.

what doing wrong?

var times = 0 var stopcounting = 10  @ibaction func buttonpressed(sender: uibutton) {    self.starttimer() }  func starttimer(){         self.timer = nstimer.scheduledtimerwithtimeinterval(1.0, target: self, selector: selector("startcounting"), userinfo: nil, repeats: true)     }  func startcounting (){  times += 1 if times < stopcounting + 1{          if counter > -1 && counter < 6 {             counting.text = string(counter++)         } else if counter == int(counting.text) {             counting.text = "0"         }      } 

import uikit  class viewcontroller: uiviewcontroller {      @iboutlet weak var strtime: uilabel!      var timer = nstimer()     var endtime: nstimeinterval = 0     var now: nstimeinterval { return nsdate().timeintervalsincereferencedate }     var times = 0      override func viewdidload() {         super.viewdidload()     }      override func didreceivememorywarning() {         super.didreceivememorywarning()     }      func updatetext(){         let time = int(ceil(endtime - now))          if time == 0 {             times++             if times == 10 {                 strtime.text = "end"                 timer.invalidate()             }             endtime = nsdate().timeintervalsincereferencedate + 5         } else {             strtime.text = time.description         }     }      @ibaction func buttonpressed(sender: uibutton) {         timer = nstimer.scheduledtimerwithtimeinterval(0.1, target: self, selector: "updatetext", userinfo: nil, repeats: true)         endtime = nsdate().timeintervalsincereferencedate + 5         sender.hidden = true     } } 

to make count 1 5 need change ceil floor, abs() , add +1

func updatetext(){     let time = int(floor(abs(endtime - - 5)))     if time == 5 {         times++         if times == 10 {             strtime.text = "10" + "     " + "0"             timer.invalidate()         }         endtime = nsdate().timeintervalsincereferencedate + 5     } else {         strtime.text = times.description + "     " + (time+1).description     } } 

Comments

Popular posts from this blog

jquery - How do you format the date used in the popover widget title of FullCalendar? -

asp.net mvc - SSO between MVCForum and Umbraco7 -

Python Tkinter keyboard using bind -