倒计时如何加毫秒
Countdown how to add milliseconds
下面的倒计时时钟从秒、分、日、月、年开始计算,我该如何添加毫秒?
local secondsLeft = 3.154e + 7
-- one year
local clockText = display.newText("00.00:00:00:00", display.contentCenterX, 80, "font1.ttf", 60)
sceneGroup:insert(bg)
sceneGroup:insert(title)
sceneGroup:insert(summary)
sceneGroup:insert(clockText)
sceneGroup:insert(clockText)
local SECONDS_IN_HOUR = 60 * 60
local SECONDS_IN_DAY = 24 * SECONDS_IN_HOUR
local SECONDS_IN_MONTH = 30 * SECONDS_IN_DAY
-- assuming an average of 30 days per month
local SECONDS_IN_YEAR = 365 * SECONDS_IN_DAY
local function updateTime()
-- decrement the number of seconds
secondsLeft = secondsLeft - 1
-- time is tracked in seconds. We need to convert it to minutes and seconds
local years = math.floor((secondsLeft / SECONDS_IN_YEAR) % 365)
local months = math.floor((secondsLeft / SECONDS_IN_MONTH) % 12)
local days = math.floor((secondsLeft / SECONDS_IN_DAY) % 30)
local hours = math.floor((secondsLeft / SECONDS_IN_HOUR) % 24)
local minutes = math.floor((secondsLeft / 60) % 60)
local seconds = secondsLeft % 60
-- make it a string using string format.
local timeDisplay = string.format("%02d·%02d·%02d·%02d·%02d·%02d", years, months, days, hours, minutes, seconds)
clockText.text = timeDisplay
end
local countDownTimer = timer.performWithDelay(100, updateTime, secondsLeft)
这是一个示例,可能对您有用:
local sceneGroup = display.newGroup()
local totalSecond = 5 * 1000 -- 5 sceonds
local clockText = display.newText("", display.contentCenterX, 80, native.systemFontBold, 30)
--sceneGroup:insert(bg)
--sceneGroup:insert(title)
--sceneGroup:insert(summary)
sceneGroup:insert(clockText)
sceneGroup:insert(clockText)
local SECONDS_IN_HOUR = 60 * 60
local SECONDS_IN_DAY = 24 * SECONDS_IN_HOUR
local SECONDS_IN_MONTH = 30 * SECONDS_IN_DAY -- assuming an average of 30 days per month
local SECONDS_IN_YEAR = 365 * SECONDS_IN_DAY
function updateTime(msLeft)
-- decrement the number of seconds
if totalSecond >= msLeft then
local secondsLeft = totalSecond - msLeft
-- time is tracked in seconds. We need to convert it to minutes and seconds
local years = math.floor((secondsLeft / 1000 / SECONDS_IN_YEAR) % 365)
local months = math.floor((secondsLeft / 1000 / SECONDS_IN_MONTH) % 12)
local days = math.floor((secondsLeft / 1000 / SECONDS_IN_DAY) % 30)
local hours = math.floor((secondsLeft / 1000 / SECONDS_IN_HOUR) % 24)
local minutes = math.floor((secondsLeft / 1000 / 60) % 60)
local seconds = math.floor(secondsLeft / 1000 % 60)
local msceonds = secondsLeft % 1000
-- make it a string using string format.
local timeDisplay = string.format("%02dY·%02dM·%02dD·%02dH·%02dM·%02dS·%03dMS", years, months, days, hours, minutes, seconds, msceonds)
clockText.text = timeDisplay
else
clockText.text = "Time's up!"
Runtime:removeEventListener("enterFrame", enterFrameListener)
end
end
function enterFrameListener(e)
updateTime(e.time)
end
Runtime:addEventListener("enterFrame", enterFrameListener)
下面的倒计时时钟从秒、分、日、月、年开始计算,我该如何添加毫秒?
local secondsLeft = 3.154e + 7
-- one year
local clockText = display.newText("00.00:00:00:00", display.contentCenterX, 80, "font1.ttf", 60)
sceneGroup:insert(bg)
sceneGroup:insert(title)
sceneGroup:insert(summary)
sceneGroup:insert(clockText)
sceneGroup:insert(clockText)
local SECONDS_IN_HOUR = 60 * 60
local SECONDS_IN_DAY = 24 * SECONDS_IN_HOUR
local SECONDS_IN_MONTH = 30 * SECONDS_IN_DAY
-- assuming an average of 30 days per month
local SECONDS_IN_YEAR = 365 * SECONDS_IN_DAY
local function updateTime()
-- decrement the number of seconds
secondsLeft = secondsLeft - 1
-- time is tracked in seconds. We need to convert it to minutes and seconds
local years = math.floor((secondsLeft / SECONDS_IN_YEAR) % 365)
local months = math.floor((secondsLeft / SECONDS_IN_MONTH) % 12)
local days = math.floor((secondsLeft / SECONDS_IN_DAY) % 30)
local hours = math.floor((secondsLeft / SECONDS_IN_HOUR) % 24)
local minutes = math.floor((secondsLeft / 60) % 60)
local seconds = secondsLeft % 60
-- make it a string using string format.
local timeDisplay = string.format("%02d·%02d·%02d·%02d·%02d·%02d", years, months, days, hours, minutes, seconds)
clockText.text = timeDisplay
end
local countDownTimer = timer.performWithDelay(100, updateTime, secondsLeft)
这是一个示例,可能对您有用:
local sceneGroup = display.newGroup()
local totalSecond = 5 * 1000 -- 5 sceonds
local clockText = display.newText("", display.contentCenterX, 80, native.systemFontBold, 30)
--sceneGroup:insert(bg)
--sceneGroup:insert(title)
--sceneGroup:insert(summary)
sceneGroup:insert(clockText)
sceneGroup:insert(clockText)
local SECONDS_IN_HOUR = 60 * 60
local SECONDS_IN_DAY = 24 * SECONDS_IN_HOUR
local SECONDS_IN_MONTH = 30 * SECONDS_IN_DAY -- assuming an average of 30 days per month
local SECONDS_IN_YEAR = 365 * SECONDS_IN_DAY
function updateTime(msLeft)
-- decrement the number of seconds
if totalSecond >= msLeft then
local secondsLeft = totalSecond - msLeft
-- time is tracked in seconds. We need to convert it to minutes and seconds
local years = math.floor((secondsLeft / 1000 / SECONDS_IN_YEAR) % 365)
local months = math.floor((secondsLeft / 1000 / SECONDS_IN_MONTH) % 12)
local days = math.floor((secondsLeft / 1000 / SECONDS_IN_DAY) % 30)
local hours = math.floor((secondsLeft / 1000 / SECONDS_IN_HOUR) % 24)
local minutes = math.floor((secondsLeft / 1000 / 60) % 60)
local seconds = math.floor(secondsLeft / 1000 % 60)
local msceonds = secondsLeft % 1000
-- make it a string using string format.
local timeDisplay = string.format("%02dY·%02dM·%02dD·%02dH·%02dM·%02dS·%03dMS", years, months, days, hours, minutes, seconds, msceonds)
clockText.text = timeDisplay
else
clockText.text = "Time's up!"
Runtime:removeEventListener("enterFrame", enterFrameListener)
end
end
function enterFrameListener(e)
updateTime(e.time)
end
Runtime:addEventListener("enterFrame", enterFrameListener)