I have an index page with iframes, two of the iframes I want to re-load in sequence, iframe1 at 20 seconds, then iframe2 20 seconds later, then iframe1 again in 20 seconds, then iframe2 again in 20 seconds, forever.
I have tried sleep() in php, , and other javascript.
Why does php and javascript not support a simple script pause?
do ( '1' < '2' ) {
sleep(20);
echo script type="text/javascript
echo 'parent.featuredBottom.location = "modules/featured-bottom.php"';
echo /script>
sleep(20);
echo script type="text/javascript
echo 'parent.randomBottom.location = "modules/random-bottom.php"';
echo /script
}
sleep(); pauses everything..
JavaScript has a function called setTimeout()
, which will wait for a specified number of milliseconds and then execute some code.
In the onload
(or document ready, whatever) of each iframe
use setTimeout()
to control the reload of the other iframe
.
// within the iframe1 content:
<body onload="iFrame1Loaded();">
...
function iFrame1Loaded() {
setTimeout(function() {
// put code here to tell iFrame2 to reload,
// parent.frames["iframe2"].something.something...
}, 20000);
}
And then basically the same thing in your iFrame2, with the 1s and 2s reversed...
More info about setTimeout()
is available at the MDN website.
Note: do not attempt to implement your own sleep()
function in JavaScript, because it will lock up the browser - the user won't be able to scroll, it won't repaint, etc., until the sleep is over.