简体   繁体   English

在页面加载 x 秒后运行某个脚本 JS

[英]Run a certain script JS after x seconds that the page is loaded

I have this script:我有这个脚本:

<script>
var Webflow = Webflow || [];
Webflow.push(function() {
  MemberStack.onReady.then(function(member) {
   if(member.memberPage){
    window.location.replace(member.memberPage);
   }else{
    setTimeout(function() { location.reload(true); }, 3000);
   }
    })
});
</script>

I would like this script to run after 5 seconds after the page loads, how could it be solved?我希望这个脚本在页面加载 5 秒后运行,如何解决?

How about window.onload along with setTimeout ? window.onloadsetTimeout怎么样?

Example:例子:

window.onload = function() {
    setTimeout(function () {
        // Do stuff here
    }, 5000);
}

or, you may also use event listeners或者,您也可以使用事件监听器

function delayAndExecute() {
   setTimeout(function () {
     // Do stuff here
   }, 5000);
}

// Everything but IE
window.addEventListener("load", function() {
    delayAndExecute();
}, false); 

// IE
window.attachEvent("onload", function() {
    delayAndExecute();
});

 <script> var Webflow = Webflow || []; window.onload = function () { setTimeout(function () { Webflow.push(function () { MemberStack.onReady.then(function (member) { if (member.memberPage) { window.location.replace(member.memberPage); } else { setTimeout(function () { location.reload(true); }, 10); } }) }); }, 5000); } </script>

So i I tried to do this but the script repeats itself every 5 seconds without taking setTimeout (function () {location.reload (true);}, 10) into consideration;所以我尝试这样做,但脚本每 5 秒重复一次,而没有考虑setTimeout (function () {location.reload (true);}, 10) I would like the script to start only the first time in 5 seconds later loading the script我希望脚本仅在 5 秒后第一次启动,然后加载脚本

5 seconds after window.onload you are pushing a function into the webflow Array. window.onload 5 秒后,您将 function 推入 webflow 数组。 But you are never calling it.但你永远不会调用它。 Add webflow[0]();添加webflow[0](); to your code;到您的代码;

var Webflow = Webflow || [];
        window.onload = function () {
        setTimeout(function () {
            Webflow.push(function () {
                MemberStack.onReady.then(function (member) {
                    if (member.memberPage) {
                        window.location.replace(member.memberPage);
                    } else {
                        setTimeout(function () { location.reload(true); }, 10);
                    }
                })
            });
            webflow[0]();
        }, 5000);
    }

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM