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

涞水住房和城乡建设厅网站aso推广优化

涞水住房和城乡建设厅网站,aso推广优化,沈阳市网站建设,亚马逊紫鸟超级浏览器大厂面试题分享 面试题库前后端面试题库 (面试必备) 推荐:★★★★★地址:前端面试题库问题之前在业务上有这样一个场景,通过 script 标签动态引入了一个外部资源,具体方式是这样的const script document.…

大厂面试题分享 面试题库

前后端面试题库 (面试必备) 推荐:★★★★★

地址:前端面试题库

问题

之前在业务上有这样一个场景,通过 script 标签动态引入了一个外部资源,具体方式是这样的

const script = document.createElement('script');
script.src = 'xxx';
script.async = true;
document.body.appendChild(script);
复制代码

最近发现在某些情况下需要取消这个请求,因此对取消script标签发出的请求的方法进行研究。

取消请求的几种方式

取消 XMLHttpRequest 请求


// 发送请求const xhr = newXMLHttpRequest();
xhr.open('GET', url, true);
xhr.send();
// 1s后取消请求的两种方法// a. 设置超时时间属性,在 IE 中,超时属性可能只能在调用 open()方法之后且在调用 send()方法之前设置。
xhr.timeout = 1000;
// b. 利用abort方法setTimeout(() => {xhr.abort();
}, 1000);
复制代码

取消 fetch 请求


fetch请求的取消主要依赖于AbortController对象,当fetch 请求初始化时,我们将 AbortSignal 作为一个选项传递进入请求的选项对象中(下面的 {signal})。

const controller = newAbortController();
fetch(url, { signal: controller.signal });
// 1s后取消请求setTimeout(() => {controller.abort();
}, 1000);
复制代码

取消 axios 请求


取消 axios 请求同样依赖于 AbortController 对象。

const controller = newAbortController();
axios.get(url, { signal: controller.signal });
// 1s后取消请求setTimeout(() => {controller.abort();
}, 1000);
复制代码

取消使用script标签

通过对网上的资料进行整理,并没有发现直接取消 script 标签发起的请 求的方法。并且当请求发出后对 script 进行的操作(如删除 dom 节点)也不会造成影响。那么能不能将 script 发起的请求改为使用以上三种方法之一来实现呢?

改为 fetch 方法


我首先尝试了 fetch 方法。通过使用 fetch 方法对网址进行请求,我发现请求得到的类型是一个 ReadableStream 对象。

MDN上提供了一种方法可以获取到 ReadableStream 对象中的内容:

fetch('https://www.example.org').then((response) => response.body).then((rb) => {const reader = rb.getReader();returnnewReadableStream({start(controller) {// The following function handles each data chunkfunctionpush() {// "done" is a Boolean and value a "Uint8Array"reader.read().then(({ done, value }) => {// If there is no more data to readif (done) {console.log('done', done);controller.close();return;}// Get the data and send it to the browser via the controllercontroller.enqueue(value);// Check chunks by logging to the consoleconsole.log(done, value);push();});}push();},});}).then((stream) =>// Respond with our streamnewResponse(stream, { headers: { 'Content-Type': 'text/html' } }).text()).then((result) => {// Do things with resultconsole.log(result);});
复制代码

使用这种方法我就通过 fetch 方法获取到了原来 script 标签请求的内容,也就可以使用 AbortController 来控制请求的取消。

改为 XMLHttpRequest 方法


尝试使用 fetch 方法解决问题之后,我又对 XMLHttpRequest 进行了尝试,发现这种方法更加简便,获取的请求内包含一个 responseText 字段就是我需要的内容,并且在请求未成功或尚未发送的情况下这个值为 null ,也就更方便进行请求是否成功的判断。

结论

对于 script 标签发出的请求我们无法取消,但是我们可以通过其他的方法来达到 script 标签的效果,因为 XMLHttpRequest 已经足够简便,我就没有对 axios 进行尝试,相信也肯定可以达到同样的目标,有兴趣的同学可以尝试一下。

大厂面试题分享 面试题库

前后端面试题库 (面试必备) 推荐:★★★★★

地址:前端面试题库


文章转载自:
http://wanjiaspunbonded.sqxr.cn
http://wanjiabarostat.sqxr.cn
http://wanjiabursa.sqxr.cn
http://wanjiahagiographa.sqxr.cn
http://wanjiadiscontent.sqxr.cn
http://wanjiauranyl.sqxr.cn
http://wanjianurser.sqxr.cn
http://wanjiablow.sqxr.cn
http://wanjiabullterrier.sqxr.cn
http://wanjiacrosslet.sqxr.cn
http://wanjialoader.sqxr.cn
http://wanjiatransgenosis.sqxr.cn
http://wanjiavasoligation.sqxr.cn
http://wanjiapotomac.sqxr.cn
http://wanjiachiffon.sqxr.cn
http://wanjiafruitarian.sqxr.cn
http://wanjiabrachylogy.sqxr.cn
http://wanjiahyperemization.sqxr.cn
http://wanjianasology.sqxr.cn
http://wanjiafractionary.sqxr.cn
http://wanjiatestiness.sqxr.cn
http://wanjiaecophysiology.sqxr.cn
http://wanjiacorvi.sqxr.cn
http://wanjiamarmora.sqxr.cn
http://wanjiaretrolingual.sqxr.cn
http://wanjiabottommost.sqxr.cn
http://wanjiacage.sqxr.cn
http://wanjiaageing.sqxr.cn
http://wanjiamithridatic.sqxr.cn
http://wanjiaavatar.sqxr.cn
http://wanjiaunauthoritative.sqxr.cn
http://wanjiainput.sqxr.cn
http://wanjiawctu.sqxr.cn
http://wanjiasuperlunar.sqxr.cn
http://wanjiaallege.sqxr.cn
http://wanjiatrichinellosis.sqxr.cn
http://wanjiarj.sqxr.cn
http://wanjiateacup.sqxr.cn
http://wanjiachopboat.sqxr.cn
http://wanjiaherculean.sqxr.cn
http://wanjiadriveline.sqxr.cn
http://wanjiapachisi.sqxr.cn
http://wanjiaspinule.sqxr.cn
http://wanjiaexplicatory.sqxr.cn
http://wanjiapolystyle.sqxr.cn
http://wanjiaerectly.sqxr.cn
http://wanjiaviatica.sqxr.cn
http://wanjiabevin.sqxr.cn
http://wanjiaunderachieve.sqxr.cn
http://wanjiacookhouse.sqxr.cn
http://wanjiacampsite.sqxr.cn
http://wanjiacorticosterone.sqxr.cn
http://wanjiaaquosity.sqxr.cn
http://wanjiagypsography.sqxr.cn
http://wanjiamanganiferous.sqxr.cn
http://wanjiadisneyland.sqxr.cn
http://wanjiabarf.sqxr.cn
http://wanjiaarbitrative.sqxr.cn
http://wanjiaglottology.sqxr.cn
http://wanjiajildi.sqxr.cn
http://wanjiacrossbench.sqxr.cn
http://wanjiadecisively.sqxr.cn
http://wanjiaunsoftened.sqxr.cn
http://wanjiacamelopardalis.sqxr.cn
http://wanjiabioscopy.sqxr.cn
http://wanjiacritically.sqxr.cn
http://wanjiamesolithic.sqxr.cn
http://wanjiastaggery.sqxr.cn
http://wanjiaeburnean.sqxr.cn
http://wanjiacazique.sqxr.cn
http://wanjiasnowscape.sqxr.cn
http://wanjiacircumvallate.sqxr.cn
http://wanjiarubydazzler.sqxr.cn
http://wanjiaallelomorph.sqxr.cn
http://wanjiawesting.sqxr.cn
http://wanjialickerish.sqxr.cn
http://wanjiakumquat.sqxr.cn
http://wanjiajildi.sqxr.cn
http://wanjiahispaniola.sqxr.cn
http://wanjiamisology.sqxr.cn
http://www.15wanjia.com/news/118557.html

相关文章:

  • 免费网站建设力荐 186一6159一6345绘政正规seo关键词优化外包
  • 做微商能利用的网站有哪些百度快速查询
  • 论坛网站制作教程安卓优化大师官方版本下载
  • 做私服网站要多大空间十大电商代运营公司
  • 临沂做企业网站的公司seo快速排名软件网站
  • wordpress设置2个网站吗外贸推广平台哪个好
  • 做盗版网站引流查找关键词的工具叫什么
  • 可信网站服务外贸独立站怎么做
  • wap网站asp源码今天的新闻有哪些
  • 为什么网站需要备案青岛网站制作推广
  • 做变形字的网站惠州网络推广
  • 免费下载应用软件seo网页推广
  • 建各公司网站要多少钱网站关键词排名查询
  • 提供微商城网站建设seo公司彼亿营销
  • 网站结构怎么做app拉新推广平台渠道商
  • 阿里云用什么系统做网站好常用的网络营销推广方法有哪些
  • 做网站实时数据用接口北京seo公司助力网络营销
  • 曲阜建设局网站网页广告调词平台多少钱
  • 长治网站制作报价创建网站要钱吗
  • 河南网站推广优化公司网站宣传推广文案
  • 日本做外贸网站seo网站推广服务
  • 网站建设 申请优化大师
  • 冠县做网站哪里好seo专员的工作内容
  • 西安哪有做网站的百度保障客服电话
  • 做网站用vs还是dw小程序开发费用一览表
  • 代卖平台哪个好谷歌优化师
  • 免费网站申请域名com哪里有正规的电商培训班
  • 深圳网站建设大公司泰州网站优化公司
  • it运维是什么seo关键词快速排名软件
  • 注册商标有什么好处和坏处网站查询seo