如果出現任何問題,有什么方法可以防止在 chrome 控制臺上拋出錯誤?我正在從 API 獲取一些資料,我希望只要用戶請求的資料不存在于 API 中,它就會忽略它并且不會在控制臺上拋出紅色錯誤,我嘗試了 `try-catch 但這對我不起作用... 代碼:
fetch('https://api.dictionaryapi.dev/api/v2/entries/en/' def)
.then(res =>{
if(res.ok){
return res.json()}
else {
speak(`i don\'t know exactly what is ${def}`)
錯誤:

uj5u.com熱心網友回復:
.catch(err => setTimeout(() => console.clear()))
……應該這樣做。該setTimeout位是必要的。它需要放在執行佇列的末尾。
看看它的作業:
fetch('//non-existent-url')
.catch(err => setTimeout(() => console.clear()))
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/369082.html
標籤:javascript 谷歌浏览器 错误处理 谷歌浏览器开发工具
