I want to merge something with this code. My goal is to add a price value in a label which is decreasing constantly ever second when countdown is started. For ex: CurrentPrice - DiscountPerSecond on first second then CurrentPrice - DiscountPerSecond*2 on second etc.. until countdown is finished.
var timerCounter:NSTimeInterval!
func updateTime(interval: NSTimeInterval) -> String {
let interval = Int(interval)
let seconds = interval % 60
let minutes = (interval / 60) % 60
let hours = interval / 3600
return String(format: "%02d:%02d:%02d", hours, minutes, seconds)
}
func startTimer(hour:Int) {
timerCounter = NSTimeInterval(hour * 60 * 60)
let aSelector : Selector = "onTimer:"
NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: aSelector, userInfo: nil, repeats: true)
}
func onTimer(timer:NSTimer!) {
countdownShow.text = updateTime(timerCounter)
timerCounter!--
if (timerCounter == 0) {
timer.invalidate()
countdownShow.text = "Event Closed!"
}
}
A few notes:
price = listPrice - (maximumDiscount * proportionOfTimeElapsed)
)NSDateComponentsFormatter
to format the remaining time.