gpt4 book ai didi

ios - 不停止定时器,重新开始 : will it cause running multiple timers in swift

转载 作者:行者123 更新时间:2023-12-03 12:58:25 26 4
gpt4 key购买 nike

我有一个像下面这样的功能:

func startTimer() {
let appDelegate = UIApplication.shared.delegate as! AppDelegate
DispatchQueue.main.async {
appDelegate.TIMER = Timer.scheduledTimer(withTimeInterval: self.REPEAT_TIME, repeats: true, block: { timer in
self.sendData(programID: self.MAIN_THREAD)
do {
try ...
}
catch let error {
...
}
})
}
}
定时器停止功能:
func stopTimer() {
let appDelegate = UIApplication.shared.delegate as! AppDelegate
if (appDelegate.TIMER != nil) {
appDelegate.TIMER?.invalidate()
appDelegate.TIMER = nil
}
}
我想知道
  • 如果我在不停止的情况下调用此函数,是否有机会启动多个计时器实例?
  • 这种情况怎么样,(这是否只停止了一个计时器并继续运行另一个计时器?)
    启动定时器→重新启动→停止
  • 最佳答案

    is there any chance to start multiple timer instances if I call this function without stopping?


    是的, startTimer将启动另一个计时器而无需停止前一个计时器,但是当您将其存储在属性 TIMER 中时您将失去对先前安排的计时器的引用,因此在结束之前将无法停止它。

    And how about this case,(does this stopped only one timer & continue running another timer?)Start the timer → Start again → Stop


    是的,它将停止上次启动的计时器,并且所有其他先前启动的计时器将继续 self.REPEAT_TIME .

    关于ios - 不停止定时器,重新开始 : will it cause running multiple timers in swift,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64328129/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com