当前位置: 首页 > news >正文

VUE接口任务轮询查询任务封装hooks

usepolling.ts

import { ref } from 'vue'interface Options {time?: numbertotalTime?: numbercount?: numbercallback?(): void
}export default function usePolling<T extends () => Promise<any>>(fun: T, options?: Options) {const result = ref(null)const error = ref(null)const {time = 2000,totalTime,count,callback = () => false} = options || ({} as Options)let timer: any = nulllet endTime: number | null = nulllet totalCount = 0const run = () => {if (endTime && endTime <= Date.now()) {end()callback()return}if (count && totalCount >= count) {end()callback()return}totalCount++timer = setTimeout(() => {fun().then((res) => {result.value = resrun()}).catch((err) => {error.value = err})}, time)}const start = () => {endTime = totalTime ? Date.now() + totalTime : nullrun()}const end = () => {setTimeout(() => {clearTimeout(timer)timer = nullendTime = nulltotalCount = 0}, 0)}return {start,end,error,result}
}

使用hook

//test.vue
import usePolling from '@/hooks/usePolling';
let pollingStart: () => void;
let pollingEnd: () => void;/**getTaskProgress方法是需要轮询的方法*/
const { start, end } = usePolling(getTaskProgress, {time: 1000 * 5,
});pollingStart = start;
pollingEnd = end;/** 查询任务进度 */
const getTaskProgress = () => {
//成功状态,结束轮询pollingEnd();
};const beganTask = ()=>{pollingStart();
}

 

http://www.dtcms.com/a/294826.html

相关文章:

  • 免费的远程电脑控制软件
  • 银河麒麟v10 更换linux内核(降内核版本5.15->5.4)
  • kanzi3.6.10 窗口插件-网页生成界面
  • istio-proxy用哪个端口代理http流量的?
  • 百度文心大模型ERNIE全面解析
  • 绿地集团携手深兰科技推动AI医诊大模型快速落地
  • AI产品经理面试宝典第48天:产品设计与用户体验优化策略
  • 从零解析DeepSeek Excel公式生成器的深度学习实现原理
  • Mybatis_1
  • Spring Bean初始化及@PostConstruc执行顺序
  • 零侵入加解密方案:Spring Boot + Jasypt + AOP实现敏感数据自动保护
  • Leetcode力扣解题记录--第54题(矩阵螺旋)
  • 表征工程中哪里用到内积 :内积vs余弦相似度--谁更胜一筹?
  • 智慧农业平台-农资农服农业数字化转型
  • 算法第三十七天:动态规划part05(第九章)
  • Golang各版本特性
  • 算法:数组part01:704. 二分查找 +977.有序数组的平方
  • ZeroMQ源码深度解析:高性能网络库的架构设计与性能优化
  • 高效编程革命:DeepSeek V3多语言支持与性能优化实战
  • 【前端】当前主流的 CSS 预处理器语言Sass / SCSS、Less、Stylus
  • C++:list(1)list的使用
  • HomeAssistant本地开发笔记
  • 「iOS」——KVO
  • MCP客户端架构与实施
  • SQL基础⑦ | 子查询
  • Linux——System V 共享内存 IPC
  • 【第十二章 W55MH32 NetBIOS示例标题】
  • ChatGPT桌面版深度解析
  • clientHeight(用于获取元素的可视高度)
  • 大致自定义文件I/O库函数的实现详解(了解即可)