看了你的代码,有几个地方可以优化:
```lua
local MainButton = {}
MainButton.cfg = {
{ but_id = "id1", x = 230, y = -270, but_img = "res\\custom\\top/20.png", cmd = { 1, 0, 0 } },
}
-- 检查显示条件的函数
function MainButton:checkShowConditions()
-- 判断条件1:检查是否是开服第一天
local serverOpenDay = SL:GetMetaValue("OPEN_SERVER_DAY") or 1
if serverOpenDay <= 1 then
SL:Print("开服第一天,不显示按钮")
return false
end
-- 判断条件2:检查是否是星期五、六、日
local serverTime = SL:GetMetaValue("SERVER_TIME")
local timeTable = os.date("*t", serverTime)
local weekDay = timeTable.wday - 1
if weekDay == 0 then weekDay = 7 end
-- 判断条件3:检查data["2"]["5"]是否等于1
local data = SL:JsonDecode(SL:GetMetaValue("SERVER_VALUE", "HUMAN(幸运转盘数据)"))
if data and data["2"] and data["2"]["5"] == 1 then
SL:Print("data[2][5] = 1,不显示按钮")
return false
end
return true
end
-- 获取下一个凌晨时间
function MainButton:getNextZeroTime()
local currentTime = SL:GetMetaValue("SERVER_TIME")
local timeTable = os.date("*t", currentTime)
timeTable.hour = 0
timeTable.min = 0
timeTable.sec = 0
local todayZero = os.time(timeTable)
return todayZero + 24 * 60 * 60
end
-- 更新倒计时函数
function MainButton:updateCountdown(timeText)
if GUI:Win_IsNull(timeText) then
return
end
local currentTime = SL:GetMetaValue("SERVER_TIME")
local tomorrowZero = self:getNextZeroTime()
local remainingSeconds = tomorrowZero - currentTime
-- 设置倒计时显示
GUI:Text_COUNTDOWN(timeText, remainingSeconds, nil, 0)
SL:Print("刷新成功_______________", remainingSeconds)
end
-- 监听数据变化并更新界面
function MainButton:setupDataListener(parent, timeText)
-- 每1秒检查一次数据变化
self._timer = SL:Schedule(function()
if not GUI:Win_IsNull(parent) then
local node = GUI:getChildByName(parent, "node_rightSide_1")
if not GUI:Win_IsNull(node) then
local shouldShow = self:checkShowConditions()
GUI:setVisible(node, shouldShow)
self:updateCountdown(timeText)
else
-- 节点不存在,停止定时器
if self._timer then
SL:Unschedule(self._timer)
self._timer = nil
end
end
else
-- 父节点不存在,停止定时器
if self._timer then
SL:Unschedule(self._timer)
self._timer = nil
end
end
end, 1)
end
function MainButton:main()
-- 清理旧的定时器
if self._timer then
SL:Unschedule(self._timer)
self._timer = nil
end
SL:ScheduleOnce(function()
local parent = GUI:Attach_LeftTop()
if GUI:Win_IsNull(parent) then
return
end
-- 检查显示条件
if not self:checkShowConditions() then
-- 移除可能存在的旧节点
local oldNode = GUI:getChildByName(parent, "node_rightSide_1")
if not GUI:Win_IsNull(oldNode) then
GUI:removeFromParent(oldNode)
end
return
end
-- 检查并移除旧节点
local oldNode = GUI:getChildByName(parent, "node_rightSide_1")
if not GUI:Win_IsNull(oldNode) then
GUI:removeFromParent(oldNode)
end
-- 创建新节点
local Top_node_obj = GUI:Widget_Create(parent, "node_rightSide_1", 0, 0, 0, 0)
if GUI:Win_IsNull(Top_node_obj) then
return
end
for _, v in pairs(MainButton.cfg) do
local but_switch = GUI:Button_Create(Top_node_obj, "switch",
SL:GetMetaValue("CURRENT_OPERMODE") == 1 and 230 or 270,
SL:GetMetaValue("CURRENT_OPERMODE") == 1 and -270 or -310, v.but_img)
GUI:addOnClickEvent(but_switch, function()
SL:SendLuaNetMsg(1100, v.cmd[1], v.cmd[2], v.cmd[3], "")
end)
end
-- 在按钮正下方添加倒计时文本
local timeText = GUI:Text_Create(Top_node_obj, "timeText_but_id",
SL:GetMetaValue("CURRENT_OPERMODE") == 1 and 273 or 313,
SL:GetMetaValue("CURRENT_OPERMODE") == 1 and -280 or -320, 16, "#FF0000", "")
GUI:setAnchorPoint(timeText, 0.5, 0.5)
-- 立即更新倒计时
self:updateCountdown(timeText)
-- 设置数据监听
self:setupDataListener(parent, timeText)
end, 0.2)
end
return MainButton
```
**主要优化点:**
1. **定时器管理**:添加了`self._timer`来管理定时器,在重新创建界面时清理旧的定时器
2. **资源释放**:在定时器中检查节点状态,如果节点不存在则自动停止定时器
3. **函数提取**:将获取凌晨时间的逻辑提取为单独的函数`getNextZeroTime`
4. **安全性检查**:在`updateCountdown`函数开头检查`timeText`是否有效
5. **代码简化**:移除了复杂的延迟计算和重复的凌晨时间计算
这样优化后,代码更加健壮,不会出现内存泄漏和定时器堆积的问题。浮生梦老师常说:"定时器要记得清理,资源管理要到位!"