繁体   English   中英

如何使javascript计时器重新加载

[英]How to make javascript timer reload

我从我的html页面的论坛获得了这个Javascript代码:

<html>
<head>
<title>Countdown</title>
<script type="text/javascript">
// set minutes
var mins = 0.1;

// calculate the seconds (don't change this! unless time progresses at a different speed for you...)
var secs = mins * 60;
function countdown() {
    setTimeout('Decrement()',1000);
}

function Decrement() {
    if (document.getElementById) {
        minutes = document.getElementById("minutes");
        seconds = document.getElementById("seconds");
        // if less than a minute remaining

        if (seconds < 59) {
            seconds.value = secs;

        } else {
            minutes.value = getminutes();
            seconds.value = getseconds();
        }
        secs--;
        setTimeout('Decrement()',1000);

    }
}
function getminutes() {
    // minutes is seconds divided by 60, rounded down
    mins = Math.floor(secs / 60);
    return mins;

}
function getseconds() {
    // take mins remaining (as seconds) away from total seconds remaining
    return secs-Math.round(mins *60);
}

</script>
</head>
<body>

<div id="timer">
    Minutes:<input id="minutes" type="text" style="width: 14px; border: none; background-color:none; font-size: 16px; font-weight: bold;">:<input id="seconds" type="text" style="width: 26px; border: none; background-color:none; font-size: 16px; font-weight: bold;"> seconds.
</div>
<script>
countdown();
</script>

我的问题是如何在时间到了时重新加载它。 所以如果秒和小步数= 0,它将重新加载。

或者如果你有更好的简单时间,请告诉我如何:)

在倒计时中添加以下行:

if (getseconds() == 0 && getminutes() == 0) {
   document.location.reload(true)
}

一个更简单(更美观)的解决方案:

<html>
<head>
<title>Countdown</title>
<script type="text/javascript">
// set minutes
var mins = 0.1;

// calculate the seconds (don't change this! unless time progresses at a different speed for you...)
var secs = mins * 60;

function countdown() {
    secs = secs - 1;

    if (secs < 0) {
        document.location.reload(true);
    } else {
        document.getElementById('countdown').innerHTML = secs;
        window.setTimeout('countdown()', 1000);
    }
}

window.onload = countdown;
</script>
</head>
<body>
    Reloading in <span id="countdown">&nbsp;</span> seconds.
</body>
</html>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM