【elemen/js】阻塞UI线程导致的开关卡顿如何优化

如上图,关闭开关时由于我的方法中有报告生成过程,会阻塞UI线程,在导出完成之前,用户界面会完全冻结,造成卡顿感

javascript 复制代码
const handlePowerSwitch = async on => {
  consoleStore.runningInfo = { ...consoleStore.info };
  const res = await appStore.changePowerAction(on);
  // 若开关机失败 开关要复原
  if (!res.success) {
    appStore.power_action = !on;
  }
  // 单机版停止调度时记录仿真报告
  if (appStore.features === 'emulator' && !on) {
    if (window.exportSimulationReport) {
      window.exportSimulationReport();
    }
  }
  
};

为了防止界面卡顿,我们需要让导出操作异步执行,不阻塞主线程。使用 setTimeout 让出控制权:

javascript 复制代码
const handlePowerSwitch = async on => {
  consoleStore.runningInfo = { ...consoleStore.info };
  const res = await appStore.changePowerAction(on);
  // 若开关机失败 开关要复原
  if (!res.success) {
    appStore.power_action = !on;
  }
  // 单机版停止调度时记录仿真报告
  if (appStore.features === 'emulator' && !on) {
    if (window.exportSimulationReport) {
      setTimeout(() => {
        window.exportSimulationReport();
      }, 100);
    }
  }
  
};
相关推荐
北冥you鱼4 分钟前
深入解析 Go 中 sync.YAML.Unmarshal:如何将 YAML 数据填充到 Config 结构体
开发语言·算法·golang
goldbin_zhang_xu15 分钟前
我用 Go 重写了一个 OpenClaw 框架:这就是 GoClaw
开发语言·后端·golang·agent框架·goclaw·双循环机制
lingran__15 分钟前
C++ STL map 与 set 底层剖析与模拟实现万字详解|基于红黑树,复刻 SGI-STL 泛型复用架构
开发语言·c++·stl·set·map·泛型编程·sgi-stl
野生技术架构师16 分钟前
Multi-agent的新趋势,从Agent Team到Agent Swarm
开发语言·人工智能
Bonnie_121519 分钟前
10-深入理解ConcurrentHashMap(JDK1.8)
java·开发语言
IMPYLH1 小时前
HTML 的 <figure> 元素
前端·javascript·html
宿6741 小时前
vue3-指令和事件处理
开发语言·前端·javascript
mqiqe1 小时前
AgentScope Java Harness:2. 上下文压缩:让长期 Agent 永不“失忆“
java·开发语言
Aurorar0rua1 小时前
CS50 x 2024 Notes Memory - 03
c语言·开发语言·学习方法
兰亭妙微UI设计公司1 小时前
兰亭妙微UIUE设计公司:海量股票数据如何清晰呈现?百度金融从搜索页到官网的系统化 UI 升级复盘
ui·百度·金融