Skip to content

Commit bf78897

Browse files
committed
Added: repeat for X number of times
1 parent 77325e6 commit bf78897

1 file changed

Lines changed: 72 additions & 6 deletions

File tree

‎Sources/SwiftyTimer.swift‎

Lines changed: 72 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,36 @@
2323
//
2424

2525
import Foundation
26+
import ObjectiveC
2627

2728
extension Timer {
2829

29-
// MARK: Schedule timers
30+
// MARK: Schedule timers
31+
32+
private struct AssociatedKeys {
33+
static var repeatCounterAddress = "repeat_counter_address"
34+
static var numberOfRepeatsAddress = "number_of_repeats_address"
35+
}
36+
37+
private var repeatCounter: Int {
38+
get {
39+
return objc_getAssociatedObject(self, &AssociatedKeys.repeatCounterAddress) as? Int ?? 0
40+
}
41+
set {
42+
43+
objc_setAssociatedObject(self,
44+
&AssociatedKeys.repeatCounterAddress,
45+
newValue,
46+
.OBJC_ASSOCIATION_RETAIN)
47+
}
48+
}
49+
50+
public class func every(_ interval: TimeInterval, `for` times: Int, _ block: @escaping (Timer) -> Void) -> Timer {
51+
52+
let timer = Timer.new(every: interval, for: times, block)
53+
timer.start()
54+
return timer
55+
}
3056

3157
/// Create and schedule a timer that will call `block` once after the specified time.
3258

@@ -56,14 +82,14 @@ extension Timer {
5682
return timer
5783
}
5884

59-
// MARK: Create timers without scheduling
85+
// MARK: Create timers without scheduling
6086

6187
/// Create a timer that will call `block` once after the specified time.
6288
///
6389
/// - Note: The timer won't fire until it's scheduled on the run loop.
6490
/// Use `NSTimer.after` to create and schedule a timer in one step.
6591
/// - Note: The `new` class function is a workaround for a crashing bug when using convenience initializers (rdar://18720947)
66-
92+
6793
public class func new(after interval: TimeInterval, _ block: @escaping () -> Void) -> Timer {
6894
return CFRunLoopTimerCreateWithHandler(kCFAllocatorDefault, CFAbsoluteTimeGetCurrent() + interval, 0, 0, 0) { _ in
6995
block()
@@ -75,20 +101,58 @@ extension Timer {
75101
/// - Note: The timer won't fire until it's scheduled on the run loop.
76102
/// Use `NSTimer.every` to create and schedule a timer in one step.
77103
/// - Note: The `new` class function is a workaround for a crashing bug when using convenience initializers (rdar://18720947)
78-
104+
79105
public class func new(every interval: TimeInterval, _ block: @escaping () -> Void) -> Timer {
80106
return CFRunLoopTimerCreateWithHandler(kCFAllocatorDefault, CFAbsoluteTimeGetCurrent() + interval, interval, 0, 0) { _ in
81107
block()
82108
}
83109
}
84110

111+
@nonobjc public class func new(every interval: TimeInterval, `for` times: Int, _ block: @escaping () -> Void) -> Timer {
112+
var timer: Timer!
113+
114+
timer = CFRunLoopTimerCreateWithHandler(kCFAllocatorDefault, CFAbsoluteTimeGetCurrent() + interval, interval, 0, 0) { _ in
115+
116+
if times > 0 {
117+
if timer.repeatCounter == times {
118+
timer.invalidate()
119+
return
120+
} else {
121+
timer.repeatCounter += 1
122+
}
123+
}
124+
125+
block()
126+
}
127+
return timer
128+
}
129+
85130
/// Create a timer that will call `block` repeatedly in specified time intervals.
86131
/// (This variant also passes the timer instance to the block)
87132
///
88133
/// - Note: The timer won't fire until it's scheduled on the run loop.
89134
/// Use `NSTimer.every` to create and schedule a timer in one step.
90135
/// - Note: The `new` class function is a workaround for a crashing bug when using convenience initializers (rdar://18720947)
91136

137+
@nonobjc public class func new(every interval: TimeInterval, `for` times: Int, _ block: @escaping (Timer) -> Void) -> Timer {
138+
var timer: Timer!
139+
140+
timer = CFRunLoopTimerCreateWithHandler(kCFAllocatorDefault, CFAbsoluteTimeGetCurrent() + interval, interval, 0, 0) { _ in
141+
142+
if times > 0 {
143+
if timer.repeatCounter == times {
144+
timer.invalidate()
145+
return
146+
} else {
147+
timer.repeatCounter += 1
148+
}
149+
}
150+
151+
block(timer)
152+
}
153+
return timer
154+
}
155+
92156
@nonobjc public class func new(every interval: TimeInterval, _ block: @escaping (Timer) -> Void) -> Timer {
93157
var timer: Timer!
94158
timer = CFRunLoopTimerCreateWithHandler(kCFAllocatorDefault, CFAbsoluteTimeGetCurrent() + interval, interval, 0, 0) { _ in
@@ -97,7 +161,7 @@ extension Timer {
97161
return timer
98162
}
99163

100-
// MARK: Manual scheduling
164+
// MARK: Manual scheduling
101165

102166
/// Schedule this timer on the run loop
103167
///
@@ -115,7 +179,7 @@ extension Timer {
115179

116180
// MARK: - Time extensions
117181

118-
extension Double {
182+
public extension Double {
119183
public var millisecond: TimeInterval { return self / 1000 }
120184
public var milliseconds: TimeInterval { return self / 1000 }
121185
public var ms: TimeInterval { return self / 1000 }
@@ -132,3 +196,5 @@ extension Double {
132196
public var day: TimeInterval { return self * 3600 * 24 }
133197
public var days: TimeInterval { return self * 3600 * 24 }
134198
}
199+
200+

0 commit comments

Comments
 (0)