• Stars
    star
    621
  • Rank 69,853 (Top 2 %)
  • Language
    Swift
  • Created over 7 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Simple and Elegant Timer

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"