检测和终止缓慢的第三方javascript请求 [英] Detecting and terminating slow third party javascript requests

查看:90
本文介绍了检测和终止缓慢的第三方javascript请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们在网站上使用跟踪像素数量来收集有关访问者的统计信息。但是,通常我们嵌入代码中的第三方javascript中的某些JavaScript正在等待服务器的响应。通常他们的服务器没有正确的响应,因此它会导致错误消息以及及时呈现网站HTML元素的问题,从而导致巨大的性能问题。

We're using number of Tracking Pixels on our website to gather stats about our visitors. However, often some of those third party javascript that we embed in code are waiting on a response from their server. Often their server is not responding properly and therefore it causes an error messages as well as problems with timely rendering the website HTML elements, which causes huge performance issues.

有没有办法检测第三方javascript什么时候在所需时间内没有返回,所以我们可以以某种方式终止对他们的服务器的呼叫,并进入下一行的代码?请注意,使用JQuery.delay()并不能解决这个问题。

Is there a way to detect when a third-party javascript doesn't return within desired time so we can somehow terminate the call to their server and proceed to the next line of code? Please note that using the JQuery.delay() is not really a solution to this problem.

处理从服务器返回的错误消息,我们可以使用Try& Catch(错误),但我不知道我们如何强制终止对外部服务器的JavaScript调用。

Handling error messages returned from their servers we can use Try & Catch(error), but I don't know how we can force termination of a javascript call to external server.

推荐答案

你可以使用缓存加载脚本:

You can load the scripts using the cache instead:

var scriptTag = document.createElement("script");
scriptTag.type = "text/cache";
scriptTag.src = "http://thirdparty.com/foo.js";
scriptTag.onreadystatechange = scriptTag.onload = function() {
    if (scriptTag.readyState == "loaded" || scriptTag.readyState == "complete")
    {
        // it's done loading
    }
}

这将导致脚本在背景中加载像一个图像;如果加载,您可以直接调用它们。需要注意的是,如果您有多个时间,可以使用所有可用的传出连接。如果您仅序列化第三方Javascript,那么最多应该消耗一个连接。

This will cause the scripts to load in the background like an image; you can then call them directly if they load. The caveat is that if you have several that time out it can consume all of the available outgoing connections. If you serialize the 3rd party Javascripts only then they should at most consume one connection.

这是 head.js 。你可能只需要使用head.js,但是您可能需要添加一些额外的逻辑来处理超时等等。

This is the technique used by head.js. You can probably just use head.js, but you may want to add some extra logic for what to do with timeouts and so on.

这篇关于检测和终止缓慢的第三方javascript请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆