Javascript自动刷新切换复选框

fud*_*ppy 3 javascript refresh reload

我希望我的访问者能够使用复选框(无iframes)切换自动页面刷新.
这是我在谷歌上发现的最相似的代码,但它是在2004年创建的,我似乎无法让它工作.
我在wordpress上使用此代码.所以问题可能在于""timerRefresh.htm?在下面检查"",我不知道要将它重命名为什么,因为我的WP页面没有以.html/.php/etc结尾......它只是以"/结尾" "
ps我知道有自动重载的浏览器扩展,我不是在寻找.

谢谢!

      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()
        }
      }
Run Code Online (Sandbox Code Playgroud)

 

<body onload="LoadPage()">
    <form name="Test">
      <input type="checkbox" name="CB1" onclick="StartTime()">
    </form>
  </body>
Run Code Online (Sandbox Code Playgroud)

D. *_*out 22

试试这个:

HTML:

<input type="checkbox" onclick="toggleAutoRefresh(this);" id="reloadCB"> Auto Refresh
Run Code Online (Sandbox Code Playgroud)

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;
Run Code Online (Sandbox Code Playgroud)