英文:
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.
<script async src="https://s.widgetwhats.com/wwwa.js" data-wwwa="1"></script>
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:
<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); //delay in milliseconds (2 seconds)
</script>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论