I need to perform a particular method (updateStatistics) when user don't type in a particular NSTextView for 2 seconds. I need this because when the text is particularly large, the updateStatistics method can cause delays in typing.
Maybe I could store the time where user ends to digit in textDidChange():
func textDidChange(_ notification: Notification) {
startDate = Date()
Timer.scheduledTimer(timeInterval: 2.0, target: self, selector: #selector(self.updateStatistics), userInfo: nil, repeats: false)
}
And then learn if 2 seconds are elapsed in the updateStatistics method:
func updateStatistics() {
let currentDate = Date()
let elapsed = currentDate.timeIntervalSince(startDate)
if elapsed >= 2 {
// update statistics code
}
}
PS There is already an answer to a somewhat similar question here, but it's for iOS and Objective-C.