I have a simple iframe
that I want to refresh every 5 seconds, without clicking somewhere or doing something at all
<iframe src="my-messages.php">
They want the user to click somewhere, I do not. I want this as an automatic process.
I have a simple iframe
that I want to refresh every 5 seconds, without clicking somewhere or doing something at all
<iframe src="my-messages.php">
They want the user to click somewhere, I do not. I want this as an automatic process.
Here's a way to do it, though if the user is doing anything within the iframe they will lose their progress.
window.setInterval(function() {
document.querySelector('iframe.reload').setAttribute('src', 'https://freesecure.timeanddate.com/clock/i5sso5fg/n1328/tluk');
}, 5000);
<iframe class="reload" src="https://freesecure.timeanddate.com/clock/i5sso5fg/n1328/tluk" frameborder="0" width="57" height="18"></iframe>
The simplest way is to use the refresh meta tag. Just add this to the head section of my-messages.php
...
<meta http-equiv="refresh" content="5" />
Alternatively, add this to my-messages.php
in order to do the same thing with Javscript...
<script>
setTimeout(function() {
window.location.reload();
}, 5000);
</script>
Use javascript to change the src of the iframe to "my-messages.php" again. This is a solution using JQuery to select the element.
<iframe src="my-messages.php" id="frame">
in your JS
var refreshMS = 10000;
var $frame = $('#frame');
var timeoutFunction = function(){
$frame.attr("src", "");
$frame.attr("src", "my-messages.php");
setTimeout(timeoutFunction, refreshMS);
};
setTimeout(timeoutFunction, refreshMS);
This basically sets the src element every 10 seconds, causing the iframe to reload its contents.
Edit: As recommended, non-jquery version
var refreshMS = 10000;
var frame = document.getElementById("frame");
var timeoutFunction = function(){
frame.setAttribute("src", "");
frame.setAttribute("src", "my-messages.php");
setTimeout(timeoutFunction, refreshMS);
};
setTimeout(timeoutFunction, refreshMS);