Several iframe reload after 30 seconds

I have a page with several iframes and I need a script that reloads the iframes after 30 seconds. Until now I only found scripts that works for only iframe and the other dont get refreshed. Is there a way to reload several at the same time?

Edit: no time out needed

$(function() {
    setInterval(refreshiframe, 5000);
});

function refreshiframe() {
    $('#testframe').attr('src', $('#testframe').attr('src'));
}

2 answers

  • answered 2019-03-13 17:49 garry man

    It's actually really simple.

    function refreshiFrames(selectors){
        //selectors is an array, it could be --  var selectors = ["#abc",".xyz"];  to get all the iframes that have Id "abc" or class "xyz"
    
        for(let i =0; i<selectors.length; i++){
            let iframes = document.querySelectorAll(selectors[i]);
            for (let j =0; j<iframes.length; j++){
                 iframes[j].src = iframes[j].src;
            }
        }
    }
    

  • answered 2019-03-13 17:51 Sam

    Reload all iframes

    let iFrames = document. getElementsByTagName('iframe');
    
    for (let i = 0; i < iFrames.length; i++){
      iFrames[i].src = iFrames[i].src;
    }
    

    Reload several iframes by class

    let iFrames = document. getElementsByClassName('element-class');
    
    for (let i = 0; i < iFrames.length; i++){
      iFrames[i].src = iFrames[i].src;
    }
    

    Reload one iframe by id

    let iFrame = document.getElementById('element-id');
    iFrame.src = iFrame.src;