Timer.scheduledTimer 没有开火
Timer.scheduledTimer not firing
我正在尝试计算车辆在两个检查站之间行驶的时间。我已经设置了以下计时器来实现这一点。
func startTimer() {
if hasStarted == true && timerStarted == false {
print("Timing started!")
gameTimer = Timer.scheduledTimer(timeInterval: 0.001, target: self, selector: (#selector(activeTiming)), userInfo: nil, repeats: true)
timerStarted = true
}
}
@objc func activeTiming() {
print("Active timing block")
if(hasFinished == false) {
gameTime = gameTime + 0.001
print("Add time succeeded")
} else {
gameTimer?.invalidate()
}
}
预期输出如下:
Timing started!
Active timing block
Add time succeeded
Add time succeeded ... etc
实际输出:
Timing started!
所以看起来 startTimer 被正确调用,但计时器没有触发 activeTiming 代码块。任何建议都会非常有帮助。预先感谢您的帮助。
发布此代码是因为我正在使用它,但我不是 Swift 方面的专家,因此您的情况可能会有所不同!
class PerformanceTest {
var name: String = ""
var tolerance: Int64 = 0
var lastTime: Int64 = 0
var thisTime: Int64 = 0
var delta: Int64 = 0
var percent: Float = 0
func setTolerance(vName: String, vTolerance: Int64) {
name = vName
tolerance = vTolerance
}
func reset() {
delta = 0
percent = Float((Float(delta) / Float(tolerance))) * 100
//high = 0
}
func start() {
lastTime = Date().toMillis()
}
func finish() {
thisTime = Date().toMillis()
let vDelta = thisTime - lastTime
if(vDelta > delta) {
delta = vDelta
percent = Float((Float(delta) / Float(tolerance))) * 100
if(delta > tolerance) {
print("Performance Indicator: \(name) Above Tolerance" + String(format: "%3.0f", percent) + "%")
}
}
}
func display() -> String {
//high = delta
//print("\(vString) Tolerance: \(tolerance) Max: \(high)")
return String(format: "%3.0f", percent) + "% |"
}
}
extension Date {
func toMillis() -> Int64! {
return Int64(self.timeIntervalSince1970 * 1000)
}
用法:
var performanceDefenseLoop = PerformanceTest()
performanceDefenseLoop.setTolerance(vName: "DefenseLoop", vTolerance: 150)
func timeToUpdateDefenses()
{
performanceDefenseLoop.start()
defesensesLoop()
performanceDefenseLoop.finish()
print("\(performanceDefenseLoop.Display())"
}
// 重置
performanceDefenseLoop.reset()
一定要在主线程启动定时器!
你的代码对我有用,但你不想这样为事件计时,因为 Timer
不那么准确,而且你浪费了大量的计算时间(读取电池)。
相反,我建议采用以下方法:
活动开始时,记录开始时间:
let startTime = Date()
活动结束时,计算经过的时间:
let elapsedTime = Date().timeIntervalSince(startTime)
elapsedTime
将以秒为单位(包括小数秒)。
我正在尝试计算车辆在两个检查站之间行驶的时间。我已经设置了以下计时器来实现这一点。
func startTimer() {
if hasStarted == true && timerStarted == false {
print("Timing started!")
gameTimer = Timer.scheduledTimer(timeInterval: 0.001, target: self, selector: (#selector(activeTiming)), userInfo: nil, repeats: true)
timerStarted = true
}
}
@objc func activeTiming() {
print("Active timing block")
if(hasFinished == false) {
gameTime = gameTime + 0.001
print("Add time succeeded")
} else {
gameTimer?.invalidate()
}
}
预期输出如下:
Timing started!
Active timing block
Add time succeeded
Add time succeeded ... etc
实际输出:
Timing started!
所以看起来 startTimer 被正确调用,但计时器没有触发 activeTiming 代码块。任何建议都会非常有帮助。预先感谢您的帮助。
发布此代码是因为我正在使用它,但我不是 Swift 方面的专家,因此您的情况可能会有所不同!
class PerformanceTest {
var name: String = ""
var tolerance: Int64 = 0
var lastTime: Int64 = 0
var thisTime: Int64 = 0
var delta: Int64 = 0
var percent: Float = 0
func setTolerance(vName: String, vTolerance: Int64) {
name = vName
tolerance = vTolerance
}
func reset() {
delta = 0
percent = Float((Float(delta) / Float(tolerance))) * 100
//high = 0
}
func start() {
lastTime = Date().toMillis()
}
func finish() {
thisTime = Date().toMillis()
let vDelta = thisTime - lastTime
if(vDelta > delta) {
delta = vDelta
percent = Float((Float(delta) / Float(tolerance))) * 100
if(delta > tolerance) {
print("Performance Indicator: \(name) Above Tolerance" + String(format: "%3.0f", percent) + "%")
}
}
}
func display() -> String {
//high = delta
//print("\(vString) Tolerance: \(tolerance) Max: \(high)")
return String(format: "%3.0f", percent) + "% |"
}
}
extension Date {
func toMillis() -> Int64! {
return Int64(self.timeIntervalSince1970 * 1000)
}
用法:
var performanceDefenseLoop = PerformanceTest()
performanceDefenseLoop.setTolerance(vName: "DefenseLoop", vTolerance: 150)
func timeToUpdateDefenses()
{
performanceDefenseLoop.start()
defesensesLoop()
performanceDefenseLoop.finish()
print("\(performanceDefenseLoop.Display())"
}
// 重置 performanceDefenseLoop.reset()
一定要在主线程启动定时器!
你的代码对我有用,但你不想这样为事件计时,因为 Timer
不那么准确,而且你浪费了大量的计算时间(读取电池)。
相反,我建议采用以下方法:
活动开始时,记录开始时间:
let startTime = Date()
活动结束时,计算经过的时间:
let elapsedTime = Date().timeIntervalSince(startTime)
elapsedTime
将以秒为单位(包括小数秒)。