Skip to content

Instantly share code, notes, and snippets.

@peatiscoding
Last active September 25, 2016 15:57
Show Gist options
  • Select an option

  • Save peatiscoding/557fdb8e1d2cbcdcc10df2f3ddad3866 to your computer and use it in GitHub Desktop.

Select an option

Save peatiscoding/557fdb8e1d2cbcdcc10df2f3ddad3866 to your computer and use it in GitHub Desktop.
JavaScript setTimeout in Swift 2.0
// Basic.swift
func setTimeout(delay:NSTimeInterval, block:()->Void) -> NSTimer {
return NSTimer.scheduledTimerWithTimeInterval(delay, target: NSBlockOperation(block: block), selector: #selector(NSOperation.main), userInfo: nil, repeats: false)
}
func setInterval(interval:NSTimeInterval, block:()->Void) -> NSTimer {
return NSTimer.scheduledTimerWithTimeInterval(interval, target: NSBlockOperation(block: block), selector: #selector(NSOperation.main), userInfo: nil, repeats: true)
}
@peatiscoding

Copy link
Copy Markdown
Author

Simple usage:

// Simple usage
let handle = setTimeout(0.35, block: { () -> Void in
    // do this stuff after 0.35 seconds
})

// Later on cancel it
handle.invalidate()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment