NSTimer 失效不能正常工作
NSTimer invalidation does not work properly
我在我的项目中使用 NSTimer:
var clickTimer: Timer?
override func mouseDown(with event: NSEvent) {
clickTimer = Timer.scheduledTimer(timeInterval: 2.0, target: self, selector: #selector(close(timer:)), userInfo: nil, repeats: false)
}
后来我在
中使它无效
override func mouseUp(with event: NSEvent) {
if let timer = self.clickTimer {
timer.invalidate()
}
}
mouseUp
里面的代码是运行但是定时器的close
方法在2秒后还是运行。为什么失效不起作用?
您正在使 self.clickTimer 的实例无效,您应该使 self.clickTimer
无效
override func mouseUp(with event: NSEvent) {
if let timer = self.clickTimer {
self.clickTimer.invalidate()
}
}
更新
我明白了,您没有启动 clickTimer!首先,如果你想这样做,你必须触发这个变量 -> clickTimer.fire()
然后当你想要时,你应该使它无效,
'Timer.scheduledTimer(timeInterval: 2.0, target: self, selector: #selector(close(timer:)), userInfo: nil, repeats: false)
当您想声明 clickTimer 但它不是您想要使其无效时自动运行!!!
尝试此代码,先使其无效,然后再删除引用。
override func mouseUp(with event: NSEvent) {
self.clickTimer?.invalidate()
self.clickTimer = nil
}
我现在明白了,您不想重新申报您的 clickTimer
所以您要做的是
if (self.clickTimer == nil) {
self.clickTimer = Timer.scheduledTimer(timeInterval: 2.0, target: self, selector: #selector(close(timer:)), userInfo: nil, repeats: false)
}
只是想一想,您正在多次重新初始化 clicktimer,前一个已成功失效但您不小心重新声明了一个新计时器.. 嗯..
为了安全起见,我还建议您在 timer.invalidate()
之后添加 self.clickTimer = nil
当多次调用mouseDown(with:)
时出现问题,您原来的计时器将被替换,因此您没有参考使其无效。
var clickTimer: Timer?
override func mouseDown(with event: NSEvent) {
clickTimer?.invalidate()
clickTimer = Timer.scheduledTimer(timeInterval: 2.0, target: self, selector: #selector(close(timer:)), userInfo: nil, repeats: false)
}
override func mouseUp(with event: NSEvent) {
clickTimer?.invalidate()
}
试试这个
private var clickTimer: NSTimer?
func functionname() {
guard clickTimer == nil else { return }
clickTimer = NSTimer.scheduledTimerWithTimeInterval(15, target: self, selector: "fetchWallPosts", userInfo: nil, repeats: true)
}
或
self. clickTimer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: (#selector(yourmethod)), userInfo: nil, repeats: true)
试试这个 -
if (self.clickTimer == nil) {
self.clickTimer = Timer.scheduledTimer(timeInterval: 2.0, target: self, selector: #selector(close(timer:)), userInfo: nil, repeats: false)
}
我在我的项目中使用 NSTimer:
var clickTimer: Timer?
override func mouseDown(with event: NSEvent) {
clickTimer = Timer.scheduledTimer(timeInterval: 2.0, target: self, selector: #selector(close(timer:)), userInfo: nil, repeats: false)
}
后来我在
中使它无效override func mouseUp(with event: NSEvent) {
if let timer = self.clickTimer {
timer.invalidate()
}
}
mouseUp
里面的代码是运行但是定时器的close
方法在2秒后还是运行。为什么失效不起作用?
您正在使 self.clickTimer 的实例无效,您应该使 self.clickTimer
无效override func mouseUp(with event: NSEvent) {
if let timer = self.clickTimer {
self.clickTimer.invalidate()
}
}
更新
我明白了,您没有启动 clickTimer!首先,如果你想这样做,你必须触发这个变量 -> clickTimer.fire()
然后当你想要时,你应该使它无效,
'Timer.scheduledTimer(timeInterval: 2.0, target: self, selector: #selector(close(timer:)), userInfo: nil, repeats: false)
当您想声明 clickTimer 但它不是您想要使其无效时自动运行!!!
尝试此代码,先使其无效,然后再删除引用。
override func mouseUp(with event: NSEvent) {
self.clickTimer?.invalidate()
self.clickTimer = nil
}
我现在明白了,您不想重新申报您的 clickTimer
所以您要做的是
if (self.clickTimer == nil) {
self.clickTimer = Timer.scheduledTimer(timeInterval: 2.0, target: self, selector: #selector(close(timer:)), userInfo: nil, repeats: false)
}
只是想一想,您正在多次重新初始化 clicktimer,前一个已成功失效但您不小心重新声明了一个新计时器.. 嗯..
为了安全起见,我还建议您在 timer.invalidate()
之后添加 self.clickTimer = nil
当多次调用mouseDown(with:)
时出现问题,您原来的计时器将被替换,因此您没有参考使其无效。
var clickTimer: Timer?
override func mouseDown(with event: NSEvent) {
clickTimer?.invalidate()
clickTimer = Timer.scheduledTimer(timeInterval: 2.0, target: self, selector: #selector(close(timer:)), userInfo: nil, repeats: false)
}
override func mouseUp(with event: NSEvent) {
clickTimer?.invalidate()
}
试试这个
private var clickTimer: NSTimer?
func functionname() {
guard clickTimer == nil else { return }
clickTimer = NSTimer.scheduledTimerWithTimeInterval(15, target: self, selector: "fetchWallPosts", userInfo: nil, repeats: true)
}
或
self. clickTimer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: (#selector(yourmethod)), userInfo: nil, repeats: true)
试试这个 -
if (self.clickTimer == nil) {
self.clickTimer = Timer.scheduledTimer(timeInterval: 2.0, target: self, selector: #selector(close(timer:)), userInfo: nil, repeats: false)
}