File tree Expand file tree Collapse file tree 2 files changed +38
-2
lines changed
Expand file tree Collapse file tree 2 files changed +38
-2
lines changed Original file line number Diff line number Diff 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
7992extension Double {
Original file line number Diff line number Diff 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
You can’t perform that action at this time.
0 commit comments