Easy way to use NSTimer in Swift for delaying code or repeating code
Clone or download
Latest commit 1935105 Feb 4, 2017
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
EasyTimer.xcodeproj - Swift 3 Update Feb 3, 2017
EasyTimer - Swift 3 Update Feb 3, 2017
EasyTimerTests - Swift 3 Update Feb 3, 2017
Sources - Swift 3 Update Feb 3, 2017
.gitignore Initial commit Mar 2, 2016
.travis.yml Update .travis.yml Mar 6, 2016
EasyTimer.podspec - updated pod for swift 3 Easy Timer release Feb 3, 2017
LICENSE Create LICENSE Mar 2, 2016
README.md Update README.md Feb 3, 2017

README.md

EasyTimer

Platforms Swift version Build Status codecov.io CocoaPods

#####Natural language syntax for an easy way to use NSTimer in Swift for delaying code or repeating code.

Heavily influenced by SwiftyTimer - Author: Radek Pietruszewski (radex)

Installation

Manually

Add the files in the 'Sources' folder to your project and you will be ready to go!

CocoaPods

EasyTimer is available through CocoaPods.

To install add the following line to your Podfile:

pod 'EasyTimer'

Usage

All EasyTimer functions are called upon a NSTimerInterval, which can be represented ruby style.

1.millisecond
1.3.millisecond
2.second
2.4.second
3.minute
3.5.minute
4.hour
4.6.hour
5.day
5.7.day

You can easily schedule three types of timers (repeating, repeating with delay, and delay) - Each of these will return a new NSTimer instance

Schedule a timer that will repeat and call block immediately

// Will repeat code in block every 2 seconds starting immediately
2.second.interval {
    print("Repeat immediately!")
}

// or with timer passed to the block
2.second.interval { (timer: NSTimer) -> Void in
    print("Repeat immediately! Do something with timer.")
}

Schedule a timer that will repeat and call block after time interval delay

// Will repeat code in block every 2 seconds after a 2 second delay
2.second.delayedInterval {
    print("Repeat after delay!")
}

// or with timer passed to the block
2.second.delayedInterval { (timer: NSTimer) -> Void in
    print("Repeat after delay! Do something with timer.")
}

Schedule a timer that will call block once after time interval

// Will execute code in block once after 2 second delay
2.second.delay {
    print("Delay ended!")
}

// or with timer passed to the block
2.second.delay { (timer: NSTimer) -> Void in
    print("Delay ended! Do something with timer.")
}

You can also choose to create a timer and start it later using timer(repeats:, delays:) which will return a newly created NSTimer instance

let timer = 3.minute.timer(repeats: true, delays: true) {
    print("Do something.")
}

// or with timer passed to the block
let timer = 3.minute.timer(repeats: true, delays: true) { (timer: NSTimer) -> Void in
    print("Do something with timer.")
}

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

timer.start()
timer.start(modes: NSDefaultRunLoopMode, NSEventTrackingRunLoopMode)

Call stop() to invalidate timers and remove them from the run loop. You can optionally pass the run loop and run loop modes:

timer.stop()
timer.stop(modes: NSDefaultRunLoopMode, NSEventTrackingRunLoopMode)

Author and Credits

Author

Niklas Fahl (fahlout) - iOS Developer (LinkedIn)

Credits

SwiftyTimer by Radek Pietruszewski (radex)

License

Copyright (c) 2016 The Board of Trustees of The University of Alabama All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

  1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
  2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
  3. Neither the name of the University nor the names of the contributors may be used to endorse or promote products derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.