function MainButton:setupDataListener(parent)
-- 每1秒检查一次数据变化
SL:Schedule(function()
if not GUI:Win_IsNull(parent) then
local node = GUI:getChildByName(parent, "node_rightSide_1")
if not GUI:Win_IsNull(node) the
```lua
function MainButton:setupDataListener(parent)
-- 将定时器ID保存到对象中,方便后续管理
self._scheduleId = 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)
-- 如果隐藏了节点,停止定时器
if not shouldShow then
if self._scheduleId then
SL:UnSchedule(self._scheduleId)
self._scheduleId = nil
end
end
end
end
end, 1)
end
```
**关键修改点:**
1. **保存定时器ID**:将 `SL:Schedule` 返回的定时器ID保存到 `self._scheduleId` 中
2. **关闭定时器**:在隐藏节点时,通过 `SL:UnSchedule(self._scheduleId)` 停止定时器
3. **清理ID**:关闭定时器后将 `self._scheduleId` 设为 `nil`,避免重复关闭
**注意事项:**
- 使用 `SL:UnSchedule()` 而不是 `GUI:unSchedule()`,因为定时器是通过SL模块创建的
- 定时器关闭后,节点不会再被显示检查,直到重新调用 `setupDataListener` 函数
- 建议在界面关闭时也清理定时器,避免内存泄漏