console.show();
const {
delivery,
interval,
quantity,
spec,
clickMode,
actionDelay
} = hamibot.env;
// 打印环境变量
toastLog('delivery = '+delivery);
toastLog('interval = '+interval);
toastLog('quantity = '+quantity);
toastLog('spec = '+spec);
toastLog('clickMode = '+clickMode);
toastLog('actionDelay = '+actionDelay);
// 创建悬浮窗并命名为 floatyWin
var floatyWin = floaty.window(
<vertical padding="16">
<button id="startBtn" text="开始任务" bg="#4CAF50" />
<button id="stopBtn" text="停止任务" bg="#F44336" enabled="false" />
<button id="exitBtn" text="退出脚本" bg="#607D8B" />
</vertical>
);
floatyWin.setAdjustEnabled(true);
floatyWin.setPosition(100, 100)
floatyWin.setSize(-2, -2);
var handlers = {
running: false,
start: function () {
toastLog('任务开始了'+handlers.running);
if(!handlers.running){
toastLog('停止刷新'+handlers.running);
}
var randomDelay = 2000 + Math.floor(Math.random() * 101);
toastLog('状态'+handlers.running);
toastLog('点击了送到家,等待'+randomDelay+'ms');
},
stop: function () {
toastLog('任务停止'+handlers.running);
if (!handlers.running) return;
// 停止任务代码
},
};
floatyWin.startBtn.click(function () {
handlers.running = true;
handlers.start();
ui.run(function () {
floatyWin.startBtn.enabled = false;
floatyWin.stopBtn.enabled = true;
});
});
floatyWin.stopBtn.click(function () {
handlers.running = false;
handlers.stop();
ui.run(function () {
floatyWin.startBtn.enabled = true;
floatyWin.stopBtn.enabled = false;
});
});
// 保持悬浮窗不被系统回收
setInterval(function () {}, 1000);
当点击start按钮后调用start方法。其中的toastLog('任务开始了'+handlers.running);
if(!handlers.running){
toastLog('停止刷新'+handlers.running);
}
var randomDelay = 2000 + Math.floor(Math.random() * 101);
toastLog('状态'+handlers.running);
toastLog('点击了送到家,等待'+randomDelay+'ms');部分改为循环刷新。直到点击了stop方法
回答
文档地址 https://docs.hamibot.com/
要实现点击 “开始任务” 后持续循环刷新,直到点击 “停止任务” 为止,你需要将 start
方法中的逻辑放入一个定时器中,并在停止时清除该定时器。
以下是修改后的完整代码:
console.show();
const {
delivery,
interval,
quantity,
spec,
clickMode,
actionDelay
} = hamibot.env;
// 打印环境变量
toastLog('delivery = '+delivery);
toastLog('interval = '+interval);
toastLog('quantity = '+quantity);
toastLog('spec = '+spec);
toastLog('clickMode = '+clickMode);
toastLog('actionDelay = '+actionDelay);
// **创建悬浮窗并命名为 floatyWin**
var floatyWin = floaty.window(
<vertical padding="16">
<button id="startBtn" text="开始任务" bg="#4CAF50" />
<button id="stopBtn" text="停止任务" bg="#F44336" enabled="false" />
<button id="exitBtn" text="退出脚本" bg="#607D8B" />
</vertical>
);
floatyWin.setAdjustEnabled(true);
floatyWin.setPosition(100, 100)
floatyWin.setSize(-2, -2);
var handlers = {
running: false,
taskInterval: null,
start: function () {
if (handlers.running) return;
handlers.running = true;
toastLog('任务开始了');
// 开始循环执行
handlers.taskInterval = setInterval(function () {
var randomDelay = 2000 + Math.floor(Math.random() * 101);
toastLog('状态:运行中');
toastLog('点击了送到家,等待 ' + randomDelay + ' ms');
}, 1000); // 每秒执行一次
},
stop: function () {
if (!handlers.running) return;
handlers.running = false;
clearInterval(handlers.taskInterval);
toastLog('任务停止');
},
};
floatyWin.startBtn.click(function () {
handlers.start();
ui.run(function () {
floatyWin.startBtn.enabled = false;
floatyWin.stopBtn.enabled = true;
});
});
floatyWin.stopBtn.click(function () {
handlers.stop();
ui.run(function () {
floatyWin.startBtn.enabled = true;
floatyWin.stopBtn.enabled = false;
});
});
// **保持悬浮窗不被系统回收**
setInterval(function () {}, 1000);
功能说明:
- 点击“开始任务”按钮后,每秒钟会生成一个新的随机延迟,并输出日志。
- 点击“停止任务”按钮后,会清除定时器并停止循环。
- 使用
setInterval()
实现循环刷新逻辑,通过clearInterval()
停止任务。
你可以根据需要调整循环间隔时间(当前是 1 秒)。