4

I want my visitors to be able to toggle an auto page refresh with a checkbox (no iframes).
This is the most similar code i found on Google, but it was created in 2004 and i cant seem to get it to work.
I am using this code on wordpress. So the issue may lie in ""timerRefresh.htm?Checked"" below, i dont know what to rename it to, as my WP page doesnt end with .html/.php/etc... it just ends with a "/"
p.s i know there are browser extensions for auto-reload, i am not looking for that.

Thank you!

      var asdf = false;
      function StartTime(){
        if(asdf)clearTimeout(asdf)
        asdf = setTimeout("RefreshPage()",5000);
      }
      function RefreshPage(){
clearTimeout(asdf)
        if(document.Test.CB1.checked)
          document.location.href= "timerRefresh.htm?Checked"
      }
      function LoadPage(){
        var findCheck = document.location.href.split("?Chec");
        if(findCheck.length == 2){
          document.Test.CB1.checked=true;
          StartTime()
        }
      }

 

<body onload="LoadPage()">
    <form name="Test">
      <input type="checkbox" name="CB1" onclick="StartTime()">
    </form>
  </body>
fudgepuppy
  • 95
  • 1
  • 3
  • 9

3 Answers3

25

Try this:

HTML:

<input type="checkbox" onclick="toggleAutoRefresh(this);" id="reloadCB"> Auto Refresh

JavaScript:

var reloading;

function checkReloading() {
    if (window.location.hash=="#autoreload") {
        reloading=setTimeout("window.location.reload();", 5000);
        document.getElementById("reloadCB").checked=true;
    }
}

function toggleAutoRefresh(cb) {
    if (cb.checked) {
        window.location.replace("#autoreload");
        reloading=setTimeout("window.location.reload();", 5000);
    } else {
        window.location.replace("#");
        clearTimeout(reloading);
    }
}

window.onload=checkReloading;
Ashley Strout
  • 6,107
  • 5
  • 25
  • 45
1

Accepted answer is too complicated and didn't quite work for me. Here is what I did:

<span>Auto Refresh</span>&nbsp;<input type="checkbox" id="autoRefreshCheckbox" value="true" checked="checked" />
<script type="text/javascript">
setInterval(function ()
{
    if (document.getElementById('autoRefreshCheckbox').checked)
    {
        location.reload();
    }
}, 60000); // interval is in milliseconds
</script>
jjxtra
  • 20,415
  • 16
  • 100
  • 140
  • I am using this and it seems to work, except for on a Raspberry Pi using Chromium. It auto refreshes and rechecks the box if you uncheck the box. But in my context I want the auto-refresh on on my Raspberry Pi, so it works for what I need. – ferzle May 06 '22 at 19:41
0

If you don't have an extention, don't include one. Simple as that.

timerRefresh.htm?Checked would be nameOfThePage?Checked

Eg.: http://www.someblog.com/somePage/ => somePage/?Checked

Ricardo Souza
  • 16,030
  • 6
  • 37
  • 69
  • Not all browsers have that extension and not everyone bothers to install it. I am including this for the CONVENIENCE of my visitors. Simple as that. Thanks – fudgepuppy Jul 10 '12 at 02:41
  • I think I wasn't clear. I meant page extention (.html or .php, etc.) no browser extention. – Ricardo Souza Jul 10 '12 at 02:53