1

can someone help me please i am trying to make this javascript start its function after a 3 second delay using time out.

I am still learning javascript and jquery and would be grateful if someone could show me where im going wrong and how to fix it.

Thank you.

<script type="text/javascript">
    setTimeout(function(){
window.onload = function showPopUp(el) {
var cvr = document.getElementById("cover")
var dlg = document.getElementById("dialog")
cvr.style.display = "block"
dlg.style.display = "block"
if (document.body.style.overflow = "hidden") {
    cvr.style.width = "1024"
    cvr.style.height = "100&#37;"
    }
}
},3000);

</script> 
1
  • You should really click here: jsbeautifier.org Commented Feb 2, 2013 at 2:00

2 Answers 2

2

So in short, you have this:

setTimeout(function () {
    window.onload = function() {
        // do stuff...
    }
}, 3000);

That's not going to work. This says, after 3 seconds, assign a handler for the window's onload event. At that point the event already (most likely) fired. So this funciton won't execute.

Instead, flip it around.

window.onload = function() {
    setTimeout(function () {
        // do stuff...
    }, 3000);
}

So here, when the window loads, you do something 3 seconds later.

Sign up to request clarification or add additional context in comments.

Comments

1

This is wrong:

setTimeout(function () {
    window.onload = function showPopUp(el) {

This assigns an onload handler after 3 seconds, by which time the window has likely already loaded.


Get rid of the window.onload = function showPopUp(el) {...} part to wait 3 seconds from when the script loaded.

setTimeout(function () {
    var cvr = document.getElementById("cover")
    var dlg = document.getElementById("dialog")
    cvr.style.display = "block"
    dlg.style.display = "block"
    if (document.body.style.overflow = "hidden") {
        cvr.style.width = "1024"
        cvr.style.height = "100&#37;"
    }
}, 3000);

Or reverse the setTimeout and window.onload to wait 3 seconds after all the resources finished loading.

window.onload = function showPopUp(el) {
    setTimeout(function () {
       // ...
    }, 3000);
};

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.