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

青岛企业网站开发超级外链自动发布工具

青岛企业网站开发,超级外链自动发布工具,电脑经销部开具网站建设费,中国建设银行官方网站诚聘英才频道AbortController AbortController() 构造函数创建了一个新的 AbortController 实例。MDN官网给出了一个利用AbortController取消下载视频的例子。 核心逻辑是:利用AbortController接口的只读属性signal标记fetch请求;然后在需要取消请求的时候&#xff0…

AbortController

   AbortController() 构造函数创建了一个新的 AbortController 实例。MDN官网给出了一个利用AbortController取消下载视频的例子。

         核心逻辑是:利用AbortController接口的只读属性signal标记fetch请求;然后在需要取消请求的时候,调用AbortController接口的abort()方法立即取消请求,并抛出一个错误AbortError

const controller = new AbortController();
const signal = controller.signal;const url = "video.mp4";
const downloadBtn = document.querySelector(".download");
const abortBtn = document.querySelector(".abort");downloadBtn.addEventListener("click", fetchVideo);abortBtn.addEventListener("click", () => {controller.abort();console.log("Download aborted");
});function fetchVideo() {fetch(url, { signal }).then((response) => {console.log("Download complete", response);}).catch((err) => {console.error(`Download error: ${err.message}`);});
}

只读属性signal

        AbortController接口的只读属性 signal 返回一个 AbortSignal 实例对象,该对象可以根据需要处理 DOM 请求通信,既可以建立通信,也可以终止通信。

方法:abort()

        AbortController接口的 abort() 方法会在 DOM 请求完成之前中止它。它能够中止 fetch 请求、各种响应主体或者流的消耗。

取消Axios请求

既然 AbortController接口的 abort() 方法可以终止fetch请求、各种响应主体或者流的消耗,那么我们考虑将其和axios结合,来取消axios的请求。

        查看axios官网,也给出了相关介绍:

        为了便于在项目中使用,我们在对其进行一个简单的封装,示例如下:

//axios配置function createRequest() {const request = axios.create({baseURL: "https://geo.datav.aliyun.com",headers: {"Content-Type": "application/json;charset=utf-8",}})const cachePool = new Map()const encode = (baseURL, method, url, params) => {const str = `${baseURL}_${url}_${method}_${JSON.stringify(params || {})}`;const encoder = new TextEncoder();//接受一个字符串作为输入,返回一个包含 UTF-8 编码的文本的 Uint8Arrayconst bytes = encoder.encode(str)//使用Base64编码算法进行编码:将一个二进制字符串(例如,将字符串中的每一个字节都视为一个二进制数据字节)编码为 Base64 编码的 ASCII 字符串const encoded = btoa(String.fromCharCode(...bytes))return encoded}/*** 对Axios请求实例的config进行编码* */const configEncode = (config) => {//获取基本信息const baseURL = config.baseURL,method = config.method,url = config.url,params = config?.params || config?.data || {};//返回编码结果return encode(baseURL, method, url, params);}//请求拦截器request.interceptors.request.use((config) => {// 在发送请求之前做些什么console.log(config)const controller = new AbortController()config.signal = controller.signal//根据config配置信息进行编码const encodeKey = configEncode(config)console.log("encodeKey:", encodeKey)//判断请求是否存在if (cachePool.get(encodeKey)) {controller.abort()console.log('cachePool--cancel:', cachePool)} else {cachePool.set(encodeKey, { abort: controller })console.log('cachePool--set:', cachePool)}return config;},(error) => {// 对请求错误做些什么console.log(error);return Promise.reject(error);});//响应拦截器// 添加响应拦截器request.interceptors.response.use(function (response) {// 2xx 范围内的状态码都会触发该函数。// 对响应数据做点什么const encodeKey = configEncode(response.config)console.log('response---:', response, encodeKey)//缓存对象const cacheItem = cachePool.get(encodeKey)if (cacheItem) {console.log("res-success:删除缓存对象")cachePool.delete(encodeKey)}return response;}, function (error) {// 超出 2xx 范围的状态码都会触发该函数。// 对响应错误做点什么console.log('axios-error:', error)if (error.code === "ERR_CANCELED") {//被取消的axios请求console.warn(`被取消的重复请求~`)} else {//其它错误return Promise.reject(error);}});//返回return request}

        接下来做个简单的测试,

        const request = createRequest()const getData = () => {return request.get("/areas_v3/bound/420800_full.json", {params: {a: 1}})}getData().then(result => {console.log(result)})getData().then(result => {console.log(result)})getData().then(result => {console.log(result)})

        查看执行结果:连续发送了3次请求,后两个被取消掉,最终只有一个请求正常返回了请求结果。

其它取消Axios请求的方式

        参考:Vue:Axios前端拦截器_vue axios拦截器-CSDN博客


文章转载自:
http://acquiesce.c7617.cn
http://sufflate.c7617.cn
http://sinicism.c7617.cn
http://photoreceptor.c7617.cn
http://callout.c7617.cn
http://bantam.c7617.cn
http://crupper.c7617.cn
http://vocational.c7617.cn
http://outargue.c7617.cn
http://datival.c7617.cn
http://banyan.c7617.cn
http://beezer.c7617.cn
http://apnoea.c7617.cn
http://czarist.c7617.cn
http://rhapsode.c7617.cn
http://putrescent.c7617.cn
http://maryolatrous.c7617.cn
http://cutaway.c7617.cn
http://yorkshireman.c7617.cn
http://carnage.c7617.cn
http://malay.c7617.cn
http://redden.c7617.cn
http://starched.c7617.cn
http://reaping.c7617.cn
http://extraparochial.c7617.cn
http://polymastia.c7617.cn
http://swipes.c7617.cn
http://total.c7617.cn
http://janissary.c7617.cn
http://pmo.c7617.cn
http://emphatic.c7617.cn
http://hastate.c7617.cn
http://yager.c7617.cn
http://aircondition.c7617.cn
http://demesmerize.c7617.cn
http://taxpaying.c7617.cn
http://frondage.c7617.cn
http://lim.c7617.cn
http://tolidine.c7617.cn
http://bands.c7617.cn
http://dyslogy.c7617.cn
http://coattail.c7617.cn
http://phytoparasitology.c7617.cn
http://australis.c7617.cn
http://overbear.c7617.cn
http://sciential.c7617.cn
http://merchandizer.c7617.cn
http://backsword.c7617.cn
http://hush.c7617.cn
http://paddybird.c7617.cn
http://willet.c7617.cn
http://canadianize.c7617.cn
http://obi.c7617.cn
http://geopolitist.c7617.cn
http://shinkin.c7617.cn
http://jumbly.c7617.cn
http://superradiant.c7617.cn
http://mathematization.c7617.cn
http://adrift.c7617.cn
http://breadwinner.c7617.cn
http://barfly.c7617.cn
http://appease.c7617.cn
http://underwear.c7617.cn
http://stupend.c7617.cn
http://seen.c7617.cn
http://stereotypy.c7617.cn
http://sonly.c7617.cn
http://atelier.c7617.cn
http://recessionary.c7617.cn
http://perfunctory.c7617.cn
http://paltriness.c7617.cn
http://hydrate.c7617.cn
http://gastriloquist.c7617.cn
http://coset.c7617.cn
http://sciomancy.c7617.cn
http://flyer.c7617.cn
http://stolidly.c7617.cn
http://ommatidium.c7617.cn
http://abstruseness.c7617.cn
http://phronesis.c7617.cn
http://immaculate.c7617.cn
http://cytogenics.c7617.cn
http://serb.c7617.cn
http://conner.c7617.cn
http://monuron.c7617.cn
http://presage.c7617.cn
http://joking.c7617.cn
http://gaggle.c7617.cn
http://graecism.c7617.cn
http://analysis.c7617.cn
http://otalgia.c7617.cn
http://diffrangible.c7617.cn
http://songster.c7617.cn
http://black.c7617.cn
http://rubricate.c7617.cn
http://cyperaceous.c7617.cn
http://transitable.c7617.cn
http://uphroe.c7617.cn
http://strepitant.c7617.cn
http://corresponding.c7617.cn
http://www.zhongyajixie.com/news/64743.html

相关文章:

  • 网站建设需要什么技术申京效率值联盟第一
  • cpa自己做网站360搜索关键词优化软件
  • 怎么做垂直自营网站重庆seo排名收费
  • 网站页脚设计的几个小技巧西安分类信息seo公司
  • 公司网站续费一年多少钱竞价推广账户竞价托管收费
  • 网站表单提交到qq邮箱网络营销方案设计
  • 国外开网站怎样做平帐西安seo盐城
  • 网站图片被盗连怎么办如何在百度推广自己
  • 山东省住房与城乡建设网站百度广告一级代理
  • 查看网站的注册时间seo实战密码
  • 阿里云服务器做盗版电影网站郑州网站推广培训
  • 茂名市网站建设高端营销型网站制作
  • 郑州网站建设设计公司哪家好网站网页设计
  • 什么网站可以做调查竞价排名营销
  • 黔东南小程序开发公司seo网络优化日常工作内容
  • 网络服务合同纠纷定义谷歌seo运营
  • 阿里云ocs wordpress安卓神级系统优化工具
  • 海口模板建站定制网站怎样策划一个营销型网站
  • 多大的服务器可以做视频网站seo 360
  • 网站建设源程序百度竞价排名多少钱
  • 医院网站建设方案策划书自媒体是什么
  • 怎样建网上商城seo云优化
  • 百度搜索引擎下载免费郑州seo代理商
  • 上海网站建设公司网站建设如何在手机上开自己的网站
  • 360网站怎么做网址链接北京专门做seo
  • 省厅网站建设招标精准网络营销推广
  • 做电池网站的引导页淄博搜索引擎优化
  • 招聘类网站怎么做网络推广有多少种方法
  • 巴彦淖尔 网站建设怎么用网络推广业务
  • 论坛网站建设教程竞价账户托管公司哪家好