Awesome Open Source
Awesome Open Source

platforms

Simple and Elegant Timer

中文介绍:打造一个优雅的Timer

Compare with NSTimer

  • No retain cycle
  • Decouple with RunLoop
  • Support GCD queue
  • Support dynamically changing interval
  • Support closure syntax

Usage

single timer

//We need to maintain a strong reference to SwiftTimer. When self dealloc, timer will dealloc too. 
self.timer = SwiftTimer(interval: .seconds(2)) {
    print("fire")
}
self.timer.start()

repeatic timer

timer = SwiftTimer.repeaticTimer(interval: .seconds(1)) {
    print("fire")
}
timer.start()

dynamically changing interval

timer = SwiftTimer.repeaticTimer(interval: .seconds(5)) { timer in
	print("doSomething")
}
timer.start()  // print doSomething every 5 seconds

func speedUp(timer: SwiftTimer) {
	timer.rescheduleRepeating(interval: .seconds(1))
}
speedUp(timer) // print doSomething every 1 second 

throttle

The closure will be called after interval you specified. It is invalid to call again in the interval.

SwiftTimer.throttle(interval: .seconds(0.5), identifier: "refresh") {
	refresh();
}

debounce

The closure will be called after interval you specified. Calling again in the interval cancels the previous call.

SwiftTimer.debounce(interval: .seconds(0.5), identifier: "search") {
	search(inputText)
}

count down timer

timer = SwiftCountDownTimer(interval: .fromSeconds(0.1), times: 10) { timer , leftTimes in
    label.text = "\(leftTimes)"
}
timer.start()

Installation

Carthage:

github "100mango/SwiftTimer"
Related Awesome Lists
Top Programming Languages
Top Projects

Get A Weekly Email With Trending Projects For These Topics
No Spam. Unsubscribe easily at any time.
Swift (65,849
Closure (3,013
Gcd (645