Search code examples
wordpressiframereload

Auto refresh/reload four iframes in Wordpress


I'm pretty NEW so on the question: I have 4 iframes on one page in WordPress showing some stats. I want to make them auto refresh (not the whole page) on certain amount of time, like every 3 second.

I've managed to do that for one only but whenever I try to add another one it doesn't work. I hope there's a simple solution.

Here's the iframe:

<iframe id="idcw" src="http://web.ubercounter.com/charts/chart5?f=jsn&ext=1&pid=01edabc9-dd3c-41db-949b-9b6ee52e0af5&cid=&fromD=2018-06-26&toD=&fromT=16:48:14&toT=<=&lg=&r=0&fh=0&th=24&u=0&rt="></iframe>

And the JS I''ve putt in the Header:

<script>

window.setInterval("reloadIFrame1();", 60000);

function reloadIFrame1() {
 document.frames["idcw"].location.reload();
}

window.setInterval("reloadIFrame2();", 60000);

function reloadIFrame2() {
 document.getElementById('idcw').src = document.getElementById('idcw').src; 
}

</script>

PS: That was the only code I found on the internet working as it had to work. I tried with my very basic knowledge to add another 3 iframes to reload but only one refreshed.

Thanks in advance!


Solution

  • Try this code

    setInterval(function(){ reloadIFrame2(); }, 5000);
    		
    function reloadIFrame2() {
    
    	var elements = document.getElementsByClassName("idcw");
    	for (var i = 0, len = elements.length; i < len; i++) {
    		elements[i].src =  elements[i].src;
    	}  
    }
    <iframe class="idcw"  src="http://web.ubercounter.com/charts/chart5?f=jsn&ext=1&pid=01edabc9-dd3c-41db-949b-9b6ee52e0af5&cid=&fromD=2018-06-26&toD=&fromT=16:48:14&toT=<=&lg=&r=0&fh=0&th=24&u=0&rt="></iframe>
    
    <iframe class="idcw"  src="http://web.ubercounter.com/charts/chart5?f=jsn&ext=1&pid=01edabc9-dd3c-41db-949b-9b6ee52e0af5&cid=&fromD=2018-06-26&toD=&fromT=16:48:14&toT=<=&lg=&r=0&fh=0&th=24&u=0&rt="></iframe>
    
    <iframe class="idcw"  src="http://web.ubercounter.com/charts/chart5?f=jsn&ext=1&pid=01edabc9-dd3c-41db-949b-9b6ee52e0af5&cid=&fromD=2018-06-26&toD=&fromT=16:48:14&toT=<=&lg=&r=0&fh=0&th=24&u=0&rt="></iframe>
    
    
    <iframe class="idcw"  src="http://web.ubercounter.com/charts/chart5?f=jsn&ext=1&pid=01edabc9-dd3c-41db-949b-9b6ee52e0af5&cid=&fromD=2018-06-26&toD=&fromT=16:48:14&toT=<=&lg=&r=0&fh=0&th=24&u=0&rt="></iframe>
    
    	

    Run this fiddle

    http://phpfiddle.org/main/code/asn7-zfvq