简体   繁体   English

如何确保延迟脚本不会阻塞其他脚本(或异步按顺序执行)?

[英]How can I ensure that a defer script does not block other scripts (or async executes in order)?

I have a web application that provides a feedback form as provided by Jira.我有一个 web 应用程序,它提供 Jira 提供的反馈表。 Jira provides a snippet of javascript to add so that a popup with a feedback form is generated. Jira 提供了 javascript 的片段来添加,以便生成带有反馈表单的弹出窗口。

The problem I have is that sometimes Jira is unreachable or very slow, and the result is that my application is made extremely slow to open because of this.我遇到的问题是,有时 Jira 无法访问或非常慢,结果是我的应用程序因此打开非常慢。 The snippet is in this form:片段是这种形式:

<script type="text/javascript" src="https://url.to.jira/one"></script>
<script type="text/javascript" src="https://url.to.jira/two"></script>

<script type="text/javascript">
window.ATL_JQ_PAGE_PROPS = {
   "triggerFunction": function(showCollectorDialog) {
      ...
   }
}
</script>

Now, my first idea was to add "defer", but my own page has its own bunch of javascript to execute.现在,我的第一个想法是添加“延迟”,但我自己的页面有自己的一堆 javascript 来执行。 With "defer", my javascript has to wait until the Jira loading is completed, leaving my page fully rendered, but with my javascript not executed.使用“延迟”,我的 javascript 必须等到 Jira 加载完成,让我的页面完全呈现,但我的 javascript 没有执行。

With "async", my javascript is loaded and the page is properly displayed even if the Jira code is slow to load, but the order of the Jira scripts is now random.使用“异步”,我的 javascript 已加载,即使 Jira 代码加载缓慢,页面也能正确显示,但 Jira 脚本的顺序现在是随机的。 The two scripts are supposed to be loaded and run in order.这两个脚本应该按顺序加载和运行。

How can I ensure that the Jira script does not block my page, and also guarantee that the Jira feedback functionality is reliably available?如何确保 Jira 脚本不会阻止我的页面,并保证 Jira 反馈功能可靠可用?

Give the first script the async attribute, watch for its load event, and when that occurs, inject the second Jira script manually, so that Jira loads in order while also letting your own code run as soon as it can without waiting for Jira.为第一个脚本提供async属性,注意其load事件,当发生这种情况时,手动注入第二个 Jira 脚本,以便 Jira 按顺序加载,同时让您自己的代码尽快运行,而无需等待 Jira。

<script async src="https://url.to.jira/one"></script>
<script>
const firstJiraTag = document.currentScript.previousElementSibling;
firstJiraTag.addEventListener('load', () => {
  document.body.appendChild(document.createElement('script')).src = "https://url.to.jira/two";
  // once the above script fires its `load` event, both are loaded
});
// add an `error` listener to firstJiraTag if you want

// put your custom script here
// it will run as soon as this script tag runs,
// without waiting for the Jira above
window.ATL_JQ_PAGE_PROPS = {
   "triggerFunction": function(showCollectorDialog) {
      ...
   }
}
</script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何推迟或异步执行此JS脚本代码? - How can I defer or async this JS script code? 使用 async 和 defer 按顺序加载脚本 - Using async and defer to load scripts in order 如何使用延迟和内联脚本保留脚本执行顺序? - How to preserve script execution order with defer and inline scripts? 如何在OpenCart中推迟或异步javascript - How can I defer or async javascript in OpenCart 如何将脚本延迟属性用于带有 Scripts.Render 的 ASP MVC 4 Bundles - How can I use the script defer attribute for ASP MVC 4 Bundles with Scripts.Render 如何确保在所有其他附加的单击处理程序之后执行特定的单击事件处理程序? - How can I ensure a particular click event handler executes after all other attached click handlers? HTML5脚本标记确实推迟等待任何以前的异步脚本 - HTML5 script tags, does defer wait on any previous async scripts 具体怎么做<script defer=“defer”> work? - How exactly does <script defer=“defer”> work? 试图了解 Javascript 脚本执行顺序。 试过异步、延迟、动态加载脚本,结果都出乎意料 - Trying to understand Javascript script execution order. Tried async, defer, dynamically loading scripts and all have unpredictable results 在我的 polyfill 和其他脚本上设置 defer 会保证它们按顺序加载吗? - Will setting defer on my polyfill and other scripts guarantee that they're loaded in order?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM