Skip to content
Please note that GitHub no longer supports your web browser.

We recommend upgrading to the latest Google Chrome or Firefox.

Learn more
Swifty API for NSTimer
Swift Ruby Objective-C
Branch: master
Clone or download
Cannot retrieve the latest commit at this time.
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
Sources building on Swift 4.2 Oct 9, 2018
SwiftyTimer.xcodeproj updated ruby version, specified Swift 4.2 in Project in addition to t… Oct 9, 2018
SwiftyTimerTests updated tests to Swift 4.2 Oct 9, 2018
.gitignore Update .gitignore to exclude Carthage artifacts from version control Nov 14, 2015
.swift-version updated Swift version file, bumped podspec to 2.1.0, removed swift li… Oct 9, 2018
.travis.yml using proper Apple TV simulator name for tvOS 12 Oct 9, 2018
CHANGELOG.md Added changelog, updated README and Podspec to reflect Swift 4.2 version Oct 15, 2018
LICENSE Update LICENSE Aug 3, 2016
Package.swift Fix Package.swift not building Jan 3, 2017
README.md Added changelog, updated README and Podspec to reflect Swift 4.2 version Oct 15, 2018
SwiftyTimer.podspec

README.md

SwiftyTimer

Platforms CI Status CocoaPods Carthage compatible Swift version

Modern Swifty API for NSTimer

SwiftyTimer allows you to instantly schedule delays and repeating timers using convenient closure syntax. It's time to get rid of Objective-C cruft.

Read Swifty APIs: NSTimer for more information about this project.

Usage

You can easily schedule repeating and non-repeating timers (repeats and delays) using Timer.every and Timer.after:

Timer.every(0.7.seconds) {
    statusItem.blink()
}

Timer.after(1.minute) {
    println("Are you still here?")
}

You can specify time intervals with these intuitive helpers:

100.ms
1.second
2.5.seconds
5.seconds
10.minutes
1.hour
2.days

You can pass method references instead of closures:

Timer.every(30.seconds, align)

Manual scheduling

If you want to make a timer object without scheduling, use new(after:) and new(every:):

let timer = Timer.new(every: 1.second) {
    println(self.status)
}

(This should be defined as an initializer, but a bug in Foundation prevents this)

Call start() to schedule timers created using new. You can optionally pass the run loop and run loop modes:

timer.start()
timer.start(modes: .defaultRunLoopMode, .eventTrackingRunLoopMode)

Invalidation

If you want to invalidate a repeating timer on some condition, you can take a Timer argument in the closure you pass in:

Timer.every(5.seconds) { (timer: Timer) in
    // do something
    
    if finished {
        timer.invalidate()
    }
}

Installation

Note: If you're running Swift 2, use SwiftyTimer v1.4.1

CocoaPods

If you're using CocoaPods, just add this line to your Podfile:

pod 'SwiftyTimer'

Install by running this command in your terminal:

pod install

Then import the library in all files where you use it:

import SwiftyTimer

Carthage

Just add to your Cartfile:

github "radex/SwiftyTimer"

Manually

Simply copy Sources/SwiftyTimer.swift to your Xcode project.

More like this

If you like SwiftyTimer, check out SwiftyUserDefaults, which applies the same swifty approach to NSUserDefaults.

You might also be interested in my blog posts which explain the design process behind those libraries:

Contributing

If you have comments, complaints or ideas for improvements, feel free to open an issue or a pull request.

Author and license

Radek Pietruszewski

SwiftyTimer is available under the MIT license. See the LICENSE file for more info.

You can’t perform that action at this time.