Alexander Perez
Alexander Perez

Reputation: 65

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?

Upvotes: 2

Views: 927

Answers (3)

Andre
Andre

Reputation: 458

5 seconds after window.onload you are pushing a function into the webflow Array. But you are never calling it. Add 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);
    }

Upvotes: 1

Alexander Perez
Alexander Perez

Reputation: 65

<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; I would like the script to start only the first time in 5 seconds later loading the script

Upvotes: 1

Lavneet
Lavneet

Reputation: 626

How about window.onload along with setTimeout ?

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();
});

Upvotes: 2

Related Questions