Javascript 自动刷新切换复选框

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11405738/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 13:05:17  来源:igfitidea点击:

Javascript Auto Refresh Toggle Checkbox

javascriptrefreshreload

提问by fudgepuppy

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.

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

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>

回答by D. Strout

Try this:

试试这个:

HTML:

HTML:

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

JavaScript:

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;

回答by jjxtra

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>

回答by Ricardo Souza

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

如果您没有扩展名,请不要包含扩展名。就那么简单。

timerRefresh.htm?Checkedwould be nameOfThePage?Checked

timerRefresh.htm?Checked将是 nameOfThePage?Checked

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

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