q-13 / timer Goto Github PK
View Code? Open in Web Editor NEWTimer on Mac
Timer on Mac
import Cocoa
class ViewController: NSViewController {
@IBOutlet weak var timerLabel: NSTextField!
@IBOutlet weak var startButton: NSButton!
let workTime: TimeInterval = 25 * 60 // 25 minutes
let shortRestTime: TimeInterval = 5 * 60 // 5 minutes
let longRestTime: TimeInterval = 15 * 60 // 15 minutes
let workCountPerRound: Int = 4
var timer: Timer?
var timeLeft: TimeInterval = 0
var isWorking: Bool = false
var workCount: Int = 0
override func viewDidLoad() {
super.viewDidLoad()
timerLabel.stringValue = stringFromTimeInterval(workTime)
}
@IBAction func startButtonClicked(_ sender: NSButton) {
if timer == nil {
startTimer()
startButton.title = "Stop"
} else {
stopTimer()
startButton.title = "Start"
}
}
func startTimer() {
if isWorking {
timeLeft = workTime
} else {
timeLeft = shortRestTime
}
timerLabel.stringValue = stringFromTimeInterval(timeLeft)
timer = Timer.scheduledTimer(withTimeInterval: 1, repeats: true) { [weak self] _ in
guard let self = self else { return }
self.timeLeft -= 1
self.timerLabel.stringValue = self.stringFromTimeInterval(self.timeLeft)
if self.timeLeft <= 0 {
self.stopTimer()
if self.isWorking {
self.workCount += 1
if self.workCount >= self.workCountPerRound {
self.timeLeft = self.longRestTime
self.workCount = 0
} else {
self.timeLeft = self.shortRestTime
}
} else {
self.timeLeft = self.workTime
}
self.isWorking = !self.isWorking
self.timerLabel.stringValue = self.stringFromTimeInterval(self.timeLeft)
self.startTimer()
}
}
}
func stopTimer() {
timer?.invalidate()
timer = nil
}
func stringFromTimeInterval(_ interval: TimeInterval) -> String {
let formatter = DateComponentsFormatter()
formatter.allowedUnits = [.minute, .second]
formatter.unitsStyle = .positional
formatter.zeroFormattingBehavior = .pad
return formatter.string(from: interval)!
}
}
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.