自2017年推出以来, AbortController
提供了一种取消fetch()
请求的强大机制。该功能超出了简单的API调用,提供了用于管理活动听众的优雅解决方案,尤其是在诸如拖放互动之类的场景中。
一个简单的示例演示了取消fetch()
请求:
const Controller = new AbortController(); const res = fetch('/',{signal:controller.signal}); controller.abort(); console.log(res); // =>承诺(被拒绝):“ domexception:用户中止请求”
AbortController
还改善了超时处理,为setTimeout
提供了更清洁的替代方法:
功能超时(持续时间,信号){ 返回新的承诺((分辨率,拒绝)=> { const handle = settimeout(解析,持续时间); 信号?.addeventListener('abort',()=> { cleartimeout(handle); 拒绝(新错误(“流产”)); }); }); } // 用法 const Controller = new AbortController(); const Promise =超时(10000,Controller.Signal); controller.abort(); console.log(Promise); // =>承诺(被拒绝):“错误:中止”
然而,重大进步在于Chrome 88对addEventListener
中AbortSignal
的支持。这允许一种简化的方法同时删除多个事件侦听器,从而取代了多个removeEventListener
调用的需求。
考虑一个拖放示例。以前,管理mousedown
, mousemove
和mouseup
事件需要使用removeEventListener
明确删除:
//使用RemoveEventListener el.AddeventListener('Mousedown',e => { if(e.buttons!== 1)返回; const onMouseMove = e => { if(e.buttons!== 1)返回; /* 工作 */ }; const onMouseUp = e => { if(E.Buttons&1)返回; Window.RemoveEventListener('Mousemove',OnMouseMove); window.removeEventListener('MouseUp',onMouseUp); }; Window.AddeventListener('Mousemove',OnMouseMove); Window.AddeventListener('MouseUp',onMouseUp); });
使用AbortController
,一个abort()
呼叫可干净地删除所有相关的听众:
//使用abortcontroller el.AddeventListener('Mousedown',e => { if(e.buttons!== 1)返回; const Controller = new AbortController(); window.addeventlistener('Mousemove',e => { if(e.buttons!== 1)返回; /* 工作 */ },{信号:controller.signal}); window.addeventListener('MouseUp',e => { if(E.Buttons&1)返回; controller.abort(); },{信号:controller.signal}); });
目前,只有Chrome 88完全支持与addEventListener
AbortSignal
集成。虽然其他浏览器支持AbortController
,但此特定功能尚未广泛实现。多填充可用于更广泛的兼容性。
以上是使用abortcontroller作为删除活动听众的替代方案的详细内容。更多信息请关注PHP中文网其他相关文章!

是的,youshouldlearnbothflexboxandgrid.1)flexboxisidealforone-demensional,flexiblelayoutslikenavigationmenus.2)gridexcelstcelsintwo-dimensional,confffferDesignssignssuchasmagagazineLayouts.3)blosebothenHancesSunHanceSlineHancesLayOutflexibilitibilitibilitibilitibilityAnderibilitibilityAndresponScormentilial anderingStruction

重构自己的代码看起来是什么样的?约翰·瑞亚(John Rhea)挑选了他写的一个旧的CSS动画,并介绍了优化它的思维过程。

CSSanimationsarenotinherentlyhardbutrequirepracticeandunderstandingofCSSpropertiesandtimingfunctions.1)Startwithsimpleanimationslikescalingabuttononhoverusingkeyframes.2)Useeasingfunctionslikecubic-bezierfornaturaleffects,suchasabounceanimation.3)For

@keyframesispopularduetoitsversatoryand and powerincreatingsmoothcssanimations.keytricksinclude:1)definingsmoothtransitionsbetnestates,2)使用AnimatingmatematingmultationmatingMultationPropertiessimultane,3)使用使用4)使用BombingeNtibalibility,4)使用BombingingWithjavofofofofofoffo

CSSCOUNTERSAREDOMANAGEAUTOMANAMBERINGINWEBDESIGNS.1)他们可以使用forterablesofcontents,ListItems,and customnumbering.2)AdvancedsincludenestednumberingSystems.3)挑战挑战InclassINCludeBrowsEccerCerceribaliblesibility andperformiballibility andperformissises.4)创造性

使用滚动阴影,尤其是对于移动设备,是克里斯以前涵盖的一个微妙的UX。杰夫(Geoff)涵盖了一种使用动画限制属性的新方法。这是另一种方式。


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

MinGW - 适用于 Windows 的极简 GNU
这个项目正在迁移到osdn.net/projects/mingw的过程中,你可以继续在那里关注我们。MinGW:GNU编译器集合(GCC)的本地Windows移植版本,可自由分发的导入库和用于构建本地Windows应用程序的头文件;包括对MSVC运行时的扩展,以支持C99功能。MinGW的所有软件都可以在64位Windows平台上运行。

ZendStudio 13.5.1 Mac
功能强大的PHP集成开发环境

SecLists
SecLists是最终安全测试人员的伙伴。它是一个包含各种类型列表的集合,这些列表在安全评估过程中经常使用,都在一个地方。SecLists通过方便地提供安全测试人员可能需要的所有列表,帮助提高安全测试的效率和生产力。列表类型包括用户名、密码、URL、模糊测试有效载荷、敏感数据模式、Web shell等等。测试人员只需将此存储库拉到新的测试机上,他就可以访问到所需的每种类型的列表。

SublimeText3 英文版
推荐:为Win版本,支持代码提示!

VSCode Windows 64位 下载
微软推出的免费、功能强大的一款IDE编辑器