Skip to content

Commit 389b418

Browse files
committed
Merge branch 'jayrhynas-master'
2 parents fcedc33 + 937f5af commit 389b418

File tree

2 files changed

+38
-2
lines changed

2 files changed

+38
-2
lines changed

Src/SwiftyTimer.swift

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,17 +63,30 @@ extension NSTimer {
6363

6464
public class func after(interval: NSTimeInterval, _ block: () -> ()) -> NSTimer {
6565
let timer = NSTimer.new(after: interval, block)
66-
NSRunLoop.currentRunLoop().addTimer(timer, forMode: NSDefaultRunLoopMode)
66+
timer.start()
6767
return timer
6868
}
6969

7070
/// Create and schedule a timer that will call `block` repeatedly in specified time intervals.
7171

7272
public class func every(interval: NSTimeInterval, _ block: () -> ()) -> NSTimer {
7373
let timer = NSTimer.new(every: interval, block)
74-
NSRunLoop.currentRunLoop().addTimer(timer, forMode: NSDefaultRunLoopMode)
74+
timer.start()
7575
return timer
7676
}
77+
78+
/// Schedule this timer on the run loop
79+
///
80+
/// By default, the timer is scheduled on the current run loop for the default mode.
81+
/// Specify `runLoop` or `modes` to override these defaults.
82+
83+
public func start(runLoop: NSRunLoop = NSRunLoop.currentRunLoop(), modes: String...) {
84+
let modes = modes.count != 0 ? modes : [NSDefaultRunLoopMode]
85+
86+
for mode in modes {
87+
runLoop.addTimer(self, forMode: mode)
88+
}
89+
}
7790
}
7891

7992
extension Double {

Tests/SwiftyTimerTests/main.swift

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,30 @@ class AppDelegate: NSObject, NSApplicationDelegate {
5050
NSRunLoop.currentRunLoop().addTimer(timer, forMode: NSDefaultRunLoopMode)
5151
}
5252

53+
var timer4: NSTimer!
54+
5355
func test5() {
56+
var fired = false
57+
timer4 = NSTimer.new(every: 0.1.seconds) {
58+
if fired {
59+
self.timer4.invalidate()
60+
self.test6()
61+
} else {
62+
fired = true
63+
}
64+
}
65+
timer4.start()
66+
}
67+
68+
func test6() {
69+
let timer = NSTimer.new(after: 0.1.seconds) {
70+
self.test7()
71+
}
72+
73+
timer.start(modes: NSDefaultRunLoopMode, NSEventTrackingRunLoopMode, runLoop: NSRunLoop.currentRunLoop())
74+
}
75+
76+
func test7() {
5477
NSTimer.after(0.1.seconds, done)
5578
}
5679

0 commit comments

Comments
 (0)