labu77
labu77

Reputation: 807

Window.onload and run after x seconds

I open a iframe after the page is fully loaded with this javascript code:

<script>
    function loadFrame() {        
    var iframe = document.getElementById("defframe");
    iframe.src = "/frame.php"
};
window.onload = loadFrame;
</script>

HTML Code

<iframe id="defframe" frameBorder="0" scrolling="no" src="about:blank" />   </iframe>

This works perfect but I have to load this iframe 10 seconds after the page is loaded.

1. wait for window.onload

2. If window.onload is ready, wait 10 seconds more before fire.

I tried:

<script>
    window.onload = function loadFrame() {        
    var iframe = document.getElementById("defframe");
    iframe.src = "/frame.php"

    setTimeout(defframe, 10000)
};   
</script>

Without success. Thank you very much

Upvotes: 1

Views: 13709

Answers (4)

Rana Ghosh
Rana Ghosh

Reputation: 4674

The simplest way to do this is leave your function as it is, just call it in different way. Please follow below code:: HTML:

<iframe id="defframe" frameBorder="0" scrolling="no" src="about:blank" >   </iframe>

JS:

function loadFrame() {
    var iframe = document.getElementById("defframe");
    iframe.src = "/frame.php"
};
window.onload = setTimeout(loadFrame, 10000);

Working JSfiddle code:: https://jsfiddle.net/rnrztx1d/1/

Upvotes: 1

Alex Slipknot
Alex Slipknot

Reputation: 2533

You need to wrap onload process in setTimeout

window.onload = function loadFrame() {
    setTimeout(function () {
        var iframe = document.getElementById("defframe");
        iframe.src = "/frame.php";
    }, 10000);
};

Upvotes: 0

Rahul
Rahul

Reputation: 18567

You can do like this,

window.onload = function loadFrame() {        
    setTimeout(function(){
          var iframe = document.getElementById("defframe");
          iframe.src = "/frame.php"

    }, 10000)
};  

in window.load you can set up setTimeout function for 10 seconds.

Alternative way,

window.onload = function() {
  setTimeout(loadFrame(), 10000);
};

function loadFrame() {
  var iframe = document.getElementById("defframe");
  iframe.src = "/frame.php"
}

Upvotes: 0

Ajay Kumar Oad
Ajay Kumar Oad

Reputation: 560

We have to put code inside the set Time out method.

HTML:

<iframe id="defframe" frameBorder="0" scrolling="no" src="about:blank" />   </iframe>

JS

window.onload = function loadDefFrame() {
 setTimeout(function () {
 loadFrame(); //call your method
},10000)};

Complete Code:

<!DOCTYPE html>
<html>
<head>
<title>Page Title</title>
</head>
<script>

  window.onload = function loadDefFrame() {
   setTimeout(function () {
   loadFrame(); //call your method
  },10000)};

  function loadFrame() {        
   var iframe = document.getElementById("defframe");
   iframe.src = "/frame.php"
  };

</script>
<body>

<iframe id="defframe" frameBorder="0" scrolling="no" src="about:blank" /></iframe>

</body>
</html>

Upvotes: 0

Related Questions