如何向包含参数的脚本标签添加延迟?

huangapple go评论136阅读模式
英文:

How do I add a delay to a script tag that includes params?

问题

我想为这段代码添加延迟,包括async和data属性。

<script async src="https://s.widgetwhats.com/wwwa.js" data-wwwa="1"></script>

我在stackoverflow上看到了这个解决方案链接在此,但它不包括参数。

英文:

I would like to add a delay for this code including the async and the data attributes.

&lt;script async src=&quot;https://s.widgetwhats.com/wwwa.js&quot; data-wwwa=&quot;1&quot;&gt;&lt;/script&gt;

I saw this solution on stackoverflow enter link description here but it doesn't include the parameters.

答案1

得分: 2

async属性的<script>表示它将在下载后立即执行,不管页面的其余部分是否已加载。

如果您想在加载<script>之前有延迟,可以删除async属性,并使用setTimeout函数来延迟执行,如以下方式:

<script>
  setTimeout(function() {
    var script = document.createElement('script');
    script.src = "https://s.widgetwhats.com/wwwa.js";
    script.setAttribute('data-wwwa', '1');
    document.body.appendChild(script);
  }, 2000); //延迟时间以毫秒为单位(2秒)
</script>
英文:

script with async attribute means it will be executed as soon as it is downloaded, regardless of whether the rest of the page is loaded.

If you want a delay before loading the script, you can remove the async attribute and use the setTimeout function to delay the execution like the following way:

&lt;script&gt;
  setTimeout(function() {
    var script = document.createElement(&#39;script&#39;);
    script.src = &quot;https://s.widgetwhats.com/wwwa.js&quot;;
    script.setAttribute(&#39;data-wwwa&#39;, &#39;1&#39;);
    document.body.appendChild(script);
  }, 2000); //delay in milliseconds (2 seconds)
&lt;/script&gt;

huangapple
  • 本文由 发表于 2023年6月5日 13:33:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/76403700.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定