5

I have this code of Javascript that i got from a forum for my html page:

<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>

My question is how can i make it reload when the time is up. so if seconds and minuets = 0, it would reload.

Or if you have a better simple times, please show me how :)

2 Answers 2

1

add the following lines in countdown:

if (getseconds() == 0 && getminutes() == 0) {
   document.location.reload(true)
}
Sign up to request clarification or add additional context in comments.

Comments

0

A more simple (and more beautiful) solution:

<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>

1 Comment

I didn't see, that you just want a reload :)

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.