弹性定时器示例

编程入门 行业动态 更新时间:2024-10-12 03:24:22
本文介绍了弹性定时器示例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试在 flex 中使用计时器.我参考了这个例子:定时器:blog.flexexamples.

I am trying to use timer in flex. I referred to this example : Timer : blog.flexexamples.

这是我想要实现的目标:

我想启动计时器,显示自计时器起过去的分钟数开始了.它应该独立于您所在的地区.(无论您在哪个区域,计时器都应该可以正常工作每个区域).

I want to start the timer, showing minutes elapsed since timer started. It should be independent of the region you are in.( irrespective of whatever zone you are in, timer should work fine in every zone).

计时器应该继续,除非我想点击某个按钮在警报框中显示经过的时间(以分钟为单位),然后计时器应从 0 开始重新开始.

Timer should continue, unless some button is clicked, where I want to show the time elapsed in minutes, in an Alert Box and then timer should start again from 0 onwards.

我尝试了我的示例,但它无法正常工作.

I tried my example, but it is not working properly.

<?xml version="1.0" encoding="utf-8"?> <mx:Application xmlns:mx="www.adobe/2006/mxml" layout="vertical" verticalAlign="middle" backgroundColor="white" creationComplete="init()"> <mx:Script> <![CDATA[ import flash.events.TimerEvent; import flash.utils.Timer; import mx.controls.Alert; private const TIMER_INTERVAL:Number = 10; private var baseTimer:int; private var t:Timer; private function init():void { t = new Timer(TIMER_INTERVAL); t.addEventListener(TimerEvent.TIMER, updateTimer); } private function updateTimer(evt:TimerEvent):void { var d:Date = new Date(getTimer()-baseTimer); var min:String = (d.minutes).toString(); var sec:String = (d.seconds).toString(); counter.text = String(min+"."+sec); } private function startTimer():void { baseTimer = getTimer(); t.start(); } private function stopTimer():void { t.stop(); } ]]> </mx:Script> <mx:ApplicationControlBar dock="true"> <mx:Button label="Start timer" click="startTimer()" /> <mx:Button label="Stop timer" click="stopTimer()" /> </mx:ApplicationControlBar> <mx:Label id="counter" fontSize="96" /> </mx:Application>

有人能告诉我是什么问题吗?怎么解决?

Can somebody tell what is the problem ? How to solve it ?

如果我在我的电脑上运行这个例子,计时器从 30.0 开始直到它达到 59.59 然后它变回 0.0 然后再次开始......现在我想要的是从 0.0 开始并继续计数分钟直到某个按钮单击...这应该适用于任何时区

EDIT : If I run this example on my pc, timer starts from 30.0 till it reaches 59.59 and then it turns back to 0.0 and then starts again......Now What I want is to start from 0.0 and continue counting minutes till some button is clicked ... and this should work in any time zones

推荐答案

您的用例不需要使用 Date() 和/或时区.您需要做的就是计算经过的秒数,Timer 为您提供了一种简单的方法来执行此操作:将间隔设置为 1000(每秒一次计数)并使用 Timer.currentCount.然后你需要做的就是计算显示的分钟和秒.下面是一个可以合并到现有 mxml 中的实现:

Your use case does not require the use of Date() and/or time zones. All you need to do is count the seconds elapsed, and Timer offers you an easy way to do this: Set the interval to 1000 (One count per second) and use Timer.currentCount. Then all you need to do is calculate minutes and seconds for display. Below is an implementation you can incorporate into your existing mxml:

private function init():void { t = new Timer(1000); t.addEventListener(TimerEvent.TIMER, updateTimer); } // it's good practice to separate event handler from functional method private function updateTimer(evt:TimerEvent):void { display (t.currentCount); } private function display ( count : int ) : void { var minutes : int = count / 60; // divide by 60 to get the minutes var seconds : int = count % 60; // use modulo operator to get the "rest" var min : String = minutes < 10 ? "0" + minutes : "" + minutes; // add leading zero if necessary var sec : String = seconds < 10 ? "0" + seconds : "" + seconds; counter.text = min+":"+sec; // no need to cast to String if you use "" + something } private function startTimer():void { t.start(); } private function stopTimer():void { t.stop(); t.reset(); display (0); // reset the display }

更多推荐

弹性定时器示例

本文发布于:2023-06-03 09:52:14,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/471242.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:定时器   示例   弹性

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!