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.
Asked
Active
Viewed 95 times
-2
-
If the input and button are within a ` – Jonathan Lonowski Oct 29 '16 at 20:54
-
1please provide more information about your question – Dennisrec Oct 29 '16 at 20:59
-
1Please note the advice given in "[How do I ask a good question?](https://stackoverflow.com/help/how-to-ask)" It's rather difficult for us to offer assistance with what we aren't able to see. Please include the most relevant snippets of code within your post. – Jonathan Lonowski Oct 29 '16 at 21:01
-
So it should start decrementing from the time you have entered in the text box to 0 – Geeky Oct 29 '16 at 21:10
-
is that what you want – Geeky Oct 29 '16 at 21:10
-
A more descriptive title to your question would be nice as well. – react_or_angluar Oct 29 '16 at 21:21
1 Answers
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