如何在倒计时中添加毫秒

下面的倒计时钟可以从秒、分钟、天、月、年开始工作,我该如何在其中添加毫秒?

local secondsLeft = 3.154e + 7
-- 一年

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
-- 假设每个月平均有30天
local SECONDS_IN_YEAR = 365 * SECONDS_IN_DAY

local function updateTime()
  -- 减少秒数

  secondsLeft = secondsLeft - 1

  -- 时间以秒为单位跟踪。我们需要将其转换为分钟和秒

  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

  -- 使用字符串格式化将其制作为字符串。
  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)
点赞
用户3974215
用户3974215

以下是一个例子,可能对你有用:

local sceneGroup = display.newGroup()

local totalSecond = 5 * 1000 -- 5秒
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 -- 假设平均每月30天
local SECONDS_IN_YEAR = 365 * SECONDS_IN_DAY

function updateTime(msLeft)
    -- 减少剩余毫秒数
    if totalSecond >= msLeft then
        local secondsLeft = totalSecond - msLeft
        -- 时间以秒为单位跟踪。我们需要将其转换为分钟和秒钟
        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
        -- 使用字符串格式将其转换为字符串。
        local timeDisplay = string.format("%02dY·%02dM·%02dD·%02dH·%02dM·%02dS·%03dMS", years, months, days, hours, minutes, seconds, msceonds)
        clockText.text = timeDisplay
    else
        clockText.text = "时间到了!"
        Runtime:removeEventListener("enterFrame", enterFrameListener)
    end
end

function enterFrameListener(e)
    updateTime(e.time)
end

Runtime:addEventListener("enterFrame", enterFrameListener)
2016-04-20 03:58:49