我想每毫秒更改一次计时器,但它没有按预期工作。
NSTimer.scheduledTimerWithTimeInterval(0.001, target: self, selector: Selector("advanceTimer:"), userInfo: nil, repeats: true);
func advanceTimer(timer: NSTimer){
self.time += 0.001;
let milliseconds = self.time * 100;
let remaingMilliseconds = Int((milliseconds % 1000) / 10);
let seconds = Int((milliseconds / 1000) % 60)
let strSeconds = String(format: "%02d", seconds)
let strFraction = String(format: "%02d", remaingMilliseconds)
timerText.text = "\(strSeconds):\(strFraction)";
}
结果是
计时器在毫秒部分 (00:100) 变为 100,然后变为 01:00 = 40 秒
邓肯方法:
var time: NSTimeInterval = 0;
var startTime: NSTimeInterval = 0;
//And your timer method...
func advanceTimer(timer: NSTimer){
//Total time since timer started, in seconds
self.time = NSDate.timeIntervalSinceReferenceDate() - startTime
println(self.time);
//The rest of your code goes here
}
override func didMoveToView(view: SKView) {
// Set anchor point
self.anchorPoint = CGPointMake(0.5, 0.5);
var startTime: NSTimeInterval
//Sart the timer
startTime = NSDate.timeIntervalSinceReferenceDate()
NSTimer.scheduledTimerWithTimeInterval(0.02,
target: self,
selector: Selector("advanceTimer:"),
userInfo: nil,
repeats: true)
}
结果:456680125.54539 第一个打印
最佳答案
正如 Martin 在他的评论中所说,计时器的分辨率为 50-100 毫秒(0.05 到 0.1 秒)。尝试以比该时间间隔更短的时间间隔运行计时器不会给出可靠的结果。此外,计时器不是实时的。它们依赖于它们所连接的运行循环,如果运行循环变得繁忙,定时器的触发就会延迟。
与其在每次计时器触发时都尝试增加计数器,不如在启动计时器时记录开始时间,然后进行一些数学运算以计算出已经过去了多少时间:
var startTime: NSTimeInterval
//Sart the timer
startTime = NSDate.timeIntervalSinceReferenceDate()
NSTimer.scheduledTimerWithTimeInterval(0.02,
target: self,
selector: Selector("advanceTimer:"),
userInfo: nil,
repeats: true)
//And your timer method...
func advanceTimer(timer: NSTimer)
{
//Total time since timer started, in seconds
self.time = NSDate.timeIntervalSinceReferenceDate() - startTime
//The rest of your code goes here
}
此代码的 Swift 3 版本如下所示:
(作为测试项目中的 View Controller 编写)
class ViewController: UIViewController {
weak var timer: Timer?
var startTime: Double = 0
var time: Double = 0
@IBOutlet weak var timeValueLabel: UILabel!
/*
When the view controller first appears, record the time and start a timer
*/
override func viewDidAppear(_ animated: Bool) {
startTime = Date().timeIntervalSinceReferenceDate
timer = Timer.scheduledTimer(timeInterval: 0.05,
target: self,
selector: #selector(advanceTimer(timer:)),
userInfo: nil,
repeats: true)
}
//When the view controller is about to disappear, invalidate the timer
override func viewWillDisappear(_ animated: Bool) {
timer?.invalidate()
}
func advanceTimer(timer: Timer) {
//Total time since timer started, in seconds
time = Date().timeIntervalSinceReferenceDate - startTime
//The rest of your code goes here
//Convert the time to a string with 2 decimal places
let timeString = String(format: "%.2f", time)
//Display the time string to a label in our view controller
timeValueLabel.text = timeString
}
}
关于xcode - 以毫秒为单位的 Swift 定时器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30983111/
我实际上是在尝试使用RVM在我的OSX10.7.5上更新ruby,并在输入以下命令后:rvminstallruby我得到了以下回复:Searchingforbinaryrubies,thismighttakesometime.Checkingrequirementsforosx.Installingrequirementsforosx.Updatingsystem.......Errorrunning'requirements_osx_brew_update_systemruby-2.0.0-p247',pleaseread/Users/username/.rvm/log/138121
在Ruby中,以毫秒为单位获取自纪元(1970)以来的当前系统时间的正确方法是什么?我试过了Time.now.to_i,好像不是我想要的结果。我需要结果显示毫秒并且使用long类型,而不是float或double。 最佳答案 (Time.now.to_f*1000).to_iTime.now.to_f显示包含十进制数字的时间。要获得毫秒数,只需将时间乘以1000。 关于ruby-以毫秒为单位获取当前系统时间,我们在StackOverflow上找到一个类似的问题:
我正在寻找一个用ruby演示计时器的在线示例,并发现了下面的代码。它按预期工作,但这个简单的程序使用30Mo内存(如Windows任务管理器中所示)和太多CPU有意义吗?非常感谢deftime_blockstart_time=Time.nowThread.new{yield}Time.now-start_timeenddefrepeat_every(seconds)whiletruedotime_spent=time_block{yield}#Tohandle-vesleepinteravalsleep(seconds-time_spent)iftime_spent
@scores_raw.eachdo|score_raw|#belowiscodeiftimewasbeingsentinmillisecondshh=((score_raw.score.to_i)/100)/3600mm=(hh-hh.to_i)*60ss=(mm-mm.to_i)*60crumbs=[hh,mm,ss]sum=crumbs.first.to_i*3600+crumbs[1].to_i*60+crumbs.last.to_i@scoressum,:hms=>hh.round.to_s+":"+mm.round.to_s+":"+ss.round.to_s}@score
我找遍了所有我能找到的地方,但似乎找不到解决这个问题的办法。我在Lion10.8.2上使用Xcode4.5.1,并尝试为Rails项目运行bundle,但它一直卡在这上面。我正在为Heroku使用Thingem。Bolanos@Jeremys-Mac-mini⦿-1.9.3fishfarm$sudogeminstalleventmachinePassword:Buildingnativeextensions.Thiscouldtakeawhile...ERROR:Errorinstallingeventmachine:ERROR:Failedtobuildgemnativeextens
TL;DR:IneedtogetthedifferencebetweenHH:MM:SS.msandHH:MM:SS.msasHH:MM:SS:ms我需要什么:这是一个棘手的问题。我正在尝试计算两个时间戳之间的差异,如下所示:In:00:00:10.520Out:00:00:23.720应该交付:Diff:00:00:13.200我想我应该将时间解析为实际的Time对象并在那里使用差异。这在前一种情况下效果很好,并返回00:0:13.200。什么不起作用:然而,对于某些人来说,这并不能正常工作,因为Ruby使用usec而不是msec:In:00:2:22.760Out:00:2:31.
有没有一种方法可以查看ruby中为类分配的内存大小?我构建了一个自定义类,我想知道它在内存中的大小。那么C语言中有没有类似sizeof()的函数呢?我只是想像这样初始化一个新类test=MyClass.new并试图找到一种方法来打印出已分配给内存的类的大小。这在ruby中甚至可能吗? 最佳答案 没有以与C相同的方式计算类大小的语言功能。对象的内存大小取决于实现。这取决于基类对象的实现。估计使用的内存也不简单。例如,如果字符串很短,则可以嵌入到RString结构中,但如果它们很长(NevercreateRubystringsl
如果您希望在Spring中启用定时任务功能,则需要在主类上添加 @EnableScheduling 注解。这样Spring才会扫描 @Scheduled 注解并执行定时任务。在大多数情况下,只需要在主类上添加 @EnableScheduling 注解即可,不需要在Service层或其他类中再次添加。以下是一个示例,演示如何在SpringBoot中启用定时任务功能:@SpringBootApplication@EnableSchedulingpublicclassApplication{publicstaticvoidmain(String[]args){SpringApplication.ru
我编写了一个非常简单的Sassmixin,用于将像素值转换为rem值(请参阅JonathanSnook的articleonthebenefitsofusingrems)。这是代码://MixinCode$base_font_size:10;//10px@mixinrem($key,$px){#{$key}:#{$px}px;#{$key}:#{$px/$base_font_size}rem;}//Includesyntaxp{@includerem(font-size,14);}//RenderedCSSp{font-size:14px;font-size:1.4rem;}这个mixi
我最近想开始使用MacRuby。我已经安装了Xcode4.2和MacRuby,但显然我遗漏了一些东西。到目前为止,在我发现的每个教程中都说,我必须从Xcode模板中选择“MacRuby应用程序”……但是没有这样的条目可用。我试过0.10和几天前发布的每晚版本。我查看了MacRuby的安装位置,我找到了Xcode3.0的模板……我必须使用这些模板吗?如何将它们导入Xcode4.2?在开始之前,我还想知道,从MacRuby开始是否安全?乍一看,我认为"is",因为有新的MacRuby书籍可用——但MacRuby网站上似乎没有太多事件(去年3月的最后一篇博客文章?)……根据我的经验,这可能是