回答
你可以使用 floaty.window
创建一个带有“开始”和“暂停”按钮的悬浮窗,并通过 JavaScript 为这两个按钮绑定点击事件。以下是一个实现示例:
// 文档地址 https://docs.hamibot.com/
var handlers = {
running: false,
start: function () {
if (this.running) return;
this.running = true;
toast('任务开始');
// 在这里执行你的任务逻辑
},
pause: function () {
if (!this.running) return;
this.running = false;
toast('任务暂停');
// 在这里停止任务逻辑
},
};
// 创建悬浮窗
var floatyWin = floaty.window(
<vertical padding="16">
<button id="startBtn" text="开始" bg="#4CAF50" />
<button id="pauseBtn" text="暂停" bg="#FF9800" enabled="false" />
</vertical>
);
// 开始按钮点击事件
floatyWin.startBtn.click(function () {
handlers.start();
ui.run(function () {
floatyWin.startBtn.enabled = false;
floatyWin.pauseBtn.enabled = true;
});
});
// 暂停按钮点击事件
floatyWin.pauseBtn.click(function () {
handlers.pause();
ui.run(function () {
floatyWin.startBtn.enabled = true;
floatyWin.pauseBtn.enabled = false;
});
});
// 保持悬浮窗不被关闭
setInterval(function () {}, 1000);
功能说明:
- 开始按钮:点击后会触发
start()
函数,并禁用自身,同时启用“暂停”按钮。 - 暂停按钮:点击后会触发
pause()
函数,并禁用自身,同时启用“开始”按钮。 - UI 更新:通过
ui.run()
来更新按钮状态以确保 UI 线程安全。
你只需要将实际的任务逻辑写入 handlers.start
和 handlers.pause
中即可完成功能扩展。
ℹ️ 本回答由 AI 生成,内容仅供参考