Javascript Auto Refresh Toggle Checkbox

馋奶兔 提交于 2019-12-12 07:38:29

问题


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>

回答1:


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;



回答2:


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>



回答3:


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



来源:https://stackoverflow.com/questions/11405738/javascript-auto-refresh-toggle-checkbox

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!