【问题标题】:iOS chrome - xhr.timeout throws error when translating pageiOS chrome - 翻译页面时 xhr.timeout 抛出错误
【发布时间】:2021-06-21 06:05:25
【问题描述】:

这是一个非常超现实的问题。当我在 iOS 上使用 chrome 时,我无法设置 xhr.timeout。它抛出InvalidAccessError: The object does not support the operation or argument.

您可以在此处复制它:https://viktorh.net/chromebug.html 在 iOS 上的 chrome 中。然后使用 chrome 的内置翻译器翻译页面 - 现在调用 XHR.timeout 失败。

有人知道为什么会这样吗?如果它是 chrome 中的错误,有人知道解决方法吗?您可以在哪里报告此类问题?

【问题讨论】:

    标签: javascript ios google-chrome xmlhttprequest google-translate


    【解决方案1】:

    这很奇怪。我可以重现这个问题,它看起来确实是 iOS 上 Chrome 中的一个小漏洞。默认情况下 XMLHttpRequest 具有无限超时,但您可以通过手动创建超时来中止请求来解决此问题。为了安全起见,当请求成功或失败时,我会在加载端清除此超时。像这样的:

             var oReq = new XMLHttpRequest();
             oReq.addEventListener("load", reqListener);
             var timeout;
             oReq.addEventListener("loadend", function() {
                // request over - clear timeout
                clearTimeout(timeout);
             });
             // set a timeout to abort the request
             timeout = setTimeout(function() {
                oReq.abort();
                // we timed out
             },20000);
    
             oReq.open("GET", "/");
    

    【讨论】:

    • 一个公平的解决方案,谢谢!在我的情况下,在这种情况下设置超时并不是很重要,所以我用 try-catch 包围了它——但这在一个大项目中变得相当笨拙!
    • 不用担心。如果这是一个大项目,您可能需要考虑使用 AJAX 库,Axios 或 Superagent 都不错,或者使用 fetch(如果您支持旧浏览器,则使用 polyfill)。将有助于使代码不那么笨重。
    猜你喜欢
    • 1970-01-01
    • 2016-11-03
    • 2017-07-30
    • 1970-01-01
    • 1970-01-01
    • 2021-09-14
    • 2018-06-11
    • 1970-01-01
    • 2016-09-07
    相关资源
    最近更新 更多