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>

3 Answers 3

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;
0
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>
1
  • 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 6, 2022 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

2
  • 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, 2012 at 2:41
  • I think I wasn't clear. I meant page extention (.html or .php, etc.) no browser extention. Jul 10, 2012 at 2:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.