Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 18 additions & 9 deletions Src/SwiftyTimer.swift
Original file line numberDiff line numberDiff line change
Expand Up@@ -24,15 +24,18 @@

import Foundation

import Foundation

private class NSTimerActor{
var block: () -> Void
var block: (NSTimer!) -> Void
var timer: NSTimer!

init(_ block: () -> Void){
init(_ block: (NSTimer!) -> Void){
self.block = block
}

@objc func fire(){
block()
block(timer)
}
}

Expand All@@ -44,32 +47,38 @@ extension NSTimer{
/// **Note:** the timer won't fire until it's scheduled on the run loop.
/// Use `NSTimer.after` to create and schedule a timer in one step.

public class func new(after interval: NSTimeInterval, _ block: () -> Void) -> NSTimer{
public class func new(after interval: NSTimeInterval, _ block: (NSTimer!) -> Void) -> NSTimer{
let timer: NSTimer!
let actor = NSTimerActor(block)
return self.init(timeInterval: interval, target: actor, selector: "fire", userInfo: nil, repeats: false)
timer = self.init(timeInterval: interval, target: actor, selector: "fire", userInfo: nil, repeats: false)
actor.timer = timer
return timer
}

/// Create a timer that will call `block` repeatedly in specified time intervals.
///
/// **Note:** the timer won't fire until it's scheduled on the run loop.
/// Use `NSTimer.every` to create and schedule a timer in one step.

public class func new(every interval: NSTimeInterval, _ block: () -> Void) -> NSTimer{
public class func new(every interval: NSTimeInterval, _ block: (NSTimer!) -> Void) -> NSTimer{
let timer: NSTimer!
let actor = NSTimerActor(block)
return self.init(timeInterval: interval, target: actor, selector: "fire", userInfo: nil, repeats: true)
timer = self.init(timeInterval: interval, target: actor, selector: "fire", userInfo: nil, repeats: true)
actor.timer = timer
return timer
}

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

public class func after(interval: NSTimeInterval, _ block: () -> Void) -> NSTimer{
public class func after(interval: NSTimeInterval, _ block: (NSTimer!) -> Void) -> NSTimer{
let timer = NSTimer.new(after: interval, block)
timer.start()
return timer
}

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

public class func every(interval: NSTimeInterval, _ block: () -> Void) -> NSTimer{
public class func every(interval: NSTimeInterval, _ block: (NSTimer!) -> Void) -> NSTimer{
let timer = NSTimer.new(every: interval, block)
timer.start()
return timer
Expand Down