-2

I want to use a text box and a button. When I write in the text box and click on the button, the time go down until it becomes 00:00:00. Then the page reloads.

Emile Bergeron
  • 17,074
  • 5
  • 83
  • 129
Ziba
  • 7
  • 1

1 Answers1

0

As far as I understood you want to have a countdown going from x down to 0, showing the current time and reloading the page once completed?

Here is what you can do:

_StartCountDown();
function _StartCountDown()
{
    setInterval(_CountDownHelper, 1000);
}

var _currentSeconds = 10;
// If this is 10, the page will be reloaded in 10 seconds after 
// calling _StartCountDown 

function _CountDownHelper()
{
    _currentSeconds--; // Decrease
    if (_currentSeconds <= 0)
    {
        // 0 reached. Reload page:
        location.reload();
    }
    // Show time in Field.
    // Let's assume you have an HTML-Element: <p id="countdownUI"></p>
    document.getElementById("countdownUI").innerHTML = secondsToHms(_currentSeconds);
}


// Convert the Seconds to time in hh:mm:ss format (like: 00:00:10)
// Source: http://stackoverflow.com/a/5539081/6764300
function secondsToHms(d)
{
    d = Number(d);
    var h = Math.floor(d / 3600);
    var m = Math.floor(d % 3600 / 60);
    var s = Math.floor(d % 3600 % 60);
    return ((h > 0 ? h + ":" + (m < 10 ? "0" : "") : "") + m + ":" + (s < 10 ? "0" : "") + s);
}
Thomas R.
  • 91
  • 6