翻牌器显示与脚本不通
-
({
// Update component parameters (更新组件参数)
// outParams: External parameter configuration(外部参数配置)
params: function(params, tool) {
// params.test = "abc" // Parameters can be updated in the script(可以在脚本中更新参数)return params // return new Promise(...) // Can return asynchronous methods (可以返回异步方法)},
effect: function(params, tool) {
let intervalId;
let currentNumber = 788;function startInterval() {
if (intervalId) {
return;
}intervalId = setInterval(() => { getDVariable().then(res => { let newNumber = res?.b || currentNumber; if (newNumber !== currentNumber) { currentNumber = newNumber; } currentNumber += 1; addEditDVariable('b', currentNumber); }); }, 24 * 60 * 60 * 1000);}
startInterval();},
})
脚本现在是这样写的,应该是24小时进行一个加一,但是预览的时候基本6秒左右就会加1,这个是为什么 -
({
// Update component parameters (更新组件参数)
// outParams: External parameter configuration(外部参数配置)
params: function(params, tool) {
// params.test = "abc" // Parameters can be updated in the script(可以在脚本中更新参数)return params
// return new Promise(...) // Can return asynchronous methods (可以返回异步方法)
},
effect: function(params, tool) {
// 清除可能存在的旧定时器
if (window.dailyIncrementTimer) {
clearInterval(window.dailyIncrementTimer);
window.dailyIncrementTimer = null;
}let isRunning = false; // 执行定时任务 async function executeTask() { if (isRunning) { console.log('任务正在执行中,跳过'); return; } isRunning = true; console.log('开始执行递增任务...'); try { // 获取数据库中的当前值 const res = await getDVariable(); const currentValue = res?.b || 788; console.log(`当前数据库中的值: ${currentValue}`); // 递增1 const newValue = currentValue + 1; // 更新到数据库 await addEditDVariable('b', newValue); console.log(`已更新为: ${newValue}`); } catch (error) { console.error('执行任务失败:', error); } finally { isRunning = false; console.log('任务执行完成'); } } // 启动定时器,使用全局变量防止重复创建 console.log('启动每日递增定时器'); window.dailyIncrementTimer = setInterval(executeTask, 60 * 1000);}
})