自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 powerincreatingsmoothcsssanimations.keytricksinclude:1)definingsmoothtransitionsbetnestates,2)使用AnimatingMultatingMultationMultationProperPertiessimultane,3)使用使用4)使用BombingeNtibalibility,4)使用CombanningWiThjavoFofofofoftofofo

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
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

SublimeText3 英文版
推薦:為Win版本,支援程式碼提示!

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

DVWA
Damn Vulnerable Web App (DVWA) 是一個PHP/MySQL的Web應用程序,非常容易受到攻擊。它的主要目標是成為安全專業人員在合法環境中測試自己的技能和工具的輔助工具,幫助Web開發人員更好地理解保護網路應用程式的過程,並幫助教師/學生在課堂環境中教授/學習Web應用程式安全性。 DVWA的目標是透過簡單直接的介面練習一些最常見的Web漏洞,難度各不相同。請注意,該軟體中

Safe Exam Browser
Safe Exam Browser是一個安全的瀏覽器環境,安全地進行線上考試。該軟體將任何電腦變成一個安全的工作站。它控制對任何實用工具的訪問,並防止學生使用未經授權的資源。

mPDF
mPDF是一個PHP庫,可以從UTF-8編碼的HTML產生PDF檔案。原作者Ian Back編寫mPDF以從他的網站上「即時」輸出PDF文件,並處理不同的語言。與原始腳本如HTML2FPDF相比,它的速度較慢,並且在使用Unicode字體時產生的檔案較大,但支援CSS樣式等,並進行了大量增強。支援幾乎所有語言,包括RTL(阿拉伯語和希伯來語)和CJK(中日韓)。支援嵌套的區塊級元素(如P、DIV),