当我打开场景时,我使用“ onAppear”执行“ Timer”以实现即时刷新时间,但是当我切换页面时,它将再次调用。如何清除上次调用的类或其他方法。
var timer: Timer {
Timer.scheduledTimer(withTimeInterval: 1, repeats: true) {_ in
self.date = Date()
let getRemind = self.time.getRemind(date: self.order.ScheduleSleep, now: self.date, remind: self.order.remind)
self.remindMinute = getRemind.minute
self.remindSecond = getRemind.second
self.isRemindTime = getRemind.isRemindTime
}
}
var body: some View {
ZStack{
Text("\(self.remindMinute):\(self.remindSecond)")
.onAppear(perform: {
_ = self.timer
})
}
}