- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试构建一个秒表,例如,它将计数到 3.0 秒,停止,然后允许我使用新的背景/标签覆盖应用程序的 View 。我的问题是我找不到让计时器在 3 秒时自行停止和暂停的方法 - 每当我编写语句时,它只会继续计数而不做任何事情。我应该将语句放在这段代码中的什么位置,我将如何编写它?
import UIKit
class ViewController: UIViewController {
var time = 0.0
var timer = Timer()
@IBOutlet weak var lbl: UILabel!
@IBAction func start(_ sender: UIButton)
{
timer = Timer.scheduledTimer(timeInterval: 0.1, target: self, selector: #selector(ViewController.action), userInfo: nil, repeats: false)
}
@IBAction func pause(_ sender: UIButton)
{
timer.invalidate()
}
@IBAction func reset(_ sender: UIButton)
{
timer.invalidate()
time = 0.0
lbl.text = ("0")
}
@objc func action()
{
time += 0.1
lbl.text = String(time)
}
}
最佳答案
首先分离您的期望。
“时钟”是从开始到现在的时间段的容器。此外,它可以“重新启动”,因此它可能需要知道每个先前的运行周期有多长,然后将其添加到“时钟”的总持续时间中
Timer
只是一种在周期性基础上运行一些代码的简单方法。因为 Timer
只保证“至少”周期,所以应该避免将其用于简单的计数器加法,因为它会导致计算漂移(对于简单的时钟,这可能没什么大不了的,但是如果你需要任何精度,最好避免它)
简单时钟
import Foundation
public class SimpleClock {
internal var startedAt: Date? = nil
internal var totalRunningTime: TimeInterval = 0 // Used for pause/resume
var isRunning: Bool = false {
didSet {
if isRunning {
startedAt = Date()
} else {
totalRunningTime += currentCycleDuration
self.startedAt = nil
}
}
}
// This is the amount of time that this cycle has been running,
// that is, the amount of time since the clock was started to now.
// It does not include other cycles
internal var currentCycleDuration: TimeInterval {
guard let startedAt = startedAt else {
return 0
}
return Date().timeIntervalSince(startedAt)
}
func reset() {
isRunning = false
totalRunningTime = 0
}
// This is the "total" amount of time the clock has been allowed
// to run for, excluding periods when the clock was paused
var duration: TimeInterval {
return totalRunningTime + currentCycleDuration
}
}
好的,这是非常基本的概念。它只是一个容器,用于记录“循环”何时开始和停止以及管理“总体”持续时间(开始/暂停/恢复循环)
这一切都很好,但我们真正想要的是某种方法来确定周期是否“超时”。
闹钟
import Foundation
class AlarmClock: SimpleClock {
var timeout: TimeInterval = 0
var hasExpired: Bool {
return duration >= timeout
}
var timeRemaining: TimeInterval {
return max(timeout - duration, 0)
}
}
所有这一切都添加了一个“超时”周期的概念,并提供了一些额外的功能,允许用户轻松确定时钟是否已过期以及剩余时间量
好的,这很好,但这是如何工作的(并帮助我们)
好吧,这是一个非常简单的例子。它有一个标签和两个按钮。一个按钮启动/暂停时钟,另一个按钮重置它。
标签显示闹钟的运行时间和剩余时间。如果他的时钟到期,它将自动重置。
该类包含一个 Timer
,它会定期“滴答”并允许代码检查闹钟的当前状态。
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var durationLabel: UILabel!
@IBOutlet weak var cycleButton: UIButton!
@IBOutlet weak var resetButton: UIButton!
let alarmClock: AlarmClock = {
let clock = AlarmClock()
clock.timeout = 10.0
return clock
}()
var timer: Timer? = nil
var durationFormatter: DateComponentsFormatter {
let formatter = DateComponentsFormatter()
formatter.allowedUnits = [.minute, .second]
formatter.unitsStyle = .abbreviated
return formatter
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func cycleClock(_ sender: Any) {
alarmClock.isRunning = !alarmClock.isRunning
if alarmClock.isRunning {
timer = Timer.scheduledTimer(timeInterval: 0.5, target: self, selector: #selector(tick), userInfo: nil, repeats: true)
} else {
timer?.invalidate()
timer = nil
}
updateDurationLabel()
updateCycleButtonState()
}
@IBAction func restartClock(_ sender: Any) {
timer?.invalidate()
timer = nil
alarmClock.reset()
updateDurationLabel()
updateCycleButtonState()
}
func updateCycleButtonState() {
if alarmClock.isRunning {
cycleButton.setTitle("Pause", for: [])
} else {
cycleButton.setTitle("Start", for: [])
}
}
func updateDurationLabel() {
durationLabel.text = "\(durationFormatter.string(from: alarmClock.duration)!)/\(durationFormatter.string(from: alarmClock.timeRemaining)!)"
}
@objc func tick() {
print("click")
updateDurationLabel()
if alarmClock.hasExpired {
restartClock(self)
}
}
}
现在,您还可以添加某种“内部”线程来定期检查时钟的状态并调用一个委托(delegate),然后可以用它来更新 UI,但这里的目的是分离关注点,并且这意味着您不会不必要地向系统添加另一个线程(并不是说您做不到,但这只是我不想添加的复杂性的一个层次;))
关于swift - 在 Swift 中以定义的时间停止计时器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51032287/
我需要在 文档就绪 触发后 5 秒调用我的函数 abc()。 这在 jQuery 中可能吗? $(document).ready(function () { //Wait 5 seconds then
我发现 System.Diagnostics.Stopwatch 类即使在很短的时间段(即 20 秒)内似乎也存在可测量的不准确性。对于编码为运行 20 秒的进程,我的进程显示耗时为 20.3+ 秒:
我正在使用 Ionic Framework 使用 Angular 构建一个 Android/iPhone 食谱应用程序。 功能之一应该是每个成分/步骤的警报/计时器。 我猜 Ionic 是基于 Apa
我有一个 JS 计时器脚本,从 20 秒开始倒计时 var count = 0; var speed = 1000; countdown = setInterval( function
我的 JavaScript 计时器有一个大问题。一切正常,除了 1 个按钮:停止!我不知道如何停止计数器上的所有内容(例如重置每个输入和计数器)。有什么想法可以解决这个问题吗?
所以我有一个 TimerTask 任务 在其 run() 中调用函数 onTimerComplete() onTimerComplete() 看起来像这样: private void onTimerC
我在 Eclipse 中的 windowbuilder 创建的 run() 方法中有计时器。 我不知道如何通过另一种方法停止该计时器。例如通过添加 if(MenuWindow.gameStarted
我对java很陌生,我试图在按下按钮时启动计时器,但无法启动它。我尝试了几行代码,但似乎没有任何效果。 我已经声明了我的标签、文本字段和计时器: private JTextField JTFHours
import java.util.Scanner; import java.util.Timer; import java.util.TimerTask; public class Boggle {
我正在尝试在 JavaScript 中获取计时器的值,因此当计时器为零时它会显示不同的内容; var local_t = new Date(); local_t.setSeconds(local_t.
我需要使用 jquery 为网站创建自定义 slider 。到目前为止,如果我单击按钮,我已经设法让它按照我想要的方式运行,但我想实现一个自动计时器,以便幻灯片在 5 秒后切换。 这是我的 JS 代码
我正在制作一个计时器,记录点击“通过”按钮上的“确定”时的最长时间(个人最好成绩)。我似乎无法弄清楚如何让计数器回到 0,我确信有一种方法可以只用 2 个按钮(开始 + 通过)来完成。我希望计时器在我
我有一个 java.util.Timer 用于限制电子邮件发送(如果最近发送了状态电子邮件,那么现在不要发送,而是创建一个计时器任务以稍后发送任何新状态)。 我想要的是确保最终发送所有排队的电子邮件,
我已经建立了一个在线考试门户,允许学生使用网络浏览器在线参加考试。 现在我还开发了计时器功能,用户必须在规定的时间范围内完成考试。我遇到的麻烦是,当我刷新页面时,计时器再次从新开始,例如 40 分钟。
我想知道在一定时间内禁用按钮的最佳方法是什么。当用户使用其他按钮转到其他页面时,定时器不会被重置,按钮仍将被禁用。 我读过很多关于异步任务、线程的不同方法。但是我非常不确定哪种方法最好。 我希望计时器
我正在制作一个测验应用程序,我想在其中显示用户在玩游戏时所用的时间。它应该采用 HH:MM:SS 格式,从 00:00:00 开始,直到他选择答案。当用户每秒播放时,计时器应该每秒更新一次。另外,我想
您好,计划为该 Activity 开发 android 倒数计时器应用程序,当用户单击开始按钮时显示计时器倒计时并显示计时器倒计时,用户将转到剩余 Activity ,即使计时器在后台运行,如果用户单
我想知道是否有一种简单的方法可以使用 PHP 在数据库中创建计时器,我想在数据库中创建该行 30 分钟后将其删除。如果这不够具体,我很抱歉,但我不知道如何在其中提供更多细节,请随意发表评论,以便我可以
我试图制作一种与我的计时器一起使用的对象。问题是,当我只有裸函数(不在对象中)时,它就可以工作。但是当我把它放在对象内部时它不起作用。 使用此代码我只能看到 00:01 当我只使用函数本身时,它工作正
这个问题已经有答案了: How to format numbers as currency strings (67 个回答) 已关闭 9 年前。 我想显示从 1 到 2500 的美元计时器。当计时器增
我是一名优秀的程序员,十分优秀!