Search code examples
javascripthtmliframepage-refresh

How can you refresh an embedded site?


I am at a loss for how to go about refreshing an embedded page using vanilla JavaScript. I have tried the following, but it does not seem to work

<html>
  <iframe src="https://flowlab.io/game/play/1987226" width="100px" height="100px" id="HI" />
  <script>
    function refreshIFrame() {
      var x = document.getElementById("HI");
      x.contentWindow.location.reload();
    }

    function sleep(milliseconds) {
      const date = Date.now();
      let currentDate = null;
      do {
        currentDate = Date.now();
      } while (currentDate - date < milliseconds);
    }
    refreshIFrame();
    sleep(1000);

  </script>

</html>


Solution

  • Not sure what you are trying to accomplish with this, but the following will work. Use setInterval to do the timing. I added a cache buster for good measure.

        <iframe src="https://flowlab.io/game/play/1987226" style="width:1000px;height:600px;" id="HI"></iframe>
    <script>
      function refreshIFrame() {
        console.log('Refresh');
        var x = document.getElementById("HI");
        //to help prevent cached reload, add a cache buster
        const cb = new Date().getTime();
        x.contentWindow.location.href = "https://flowlab.io/game/play/1987226?cb=" + cb;
      }
    
      setInterval(function() {
        refreshIFrame();
      }, 1000)
    
    </script>