使用js

时间:2016-10-29 20:49:41

标签: javascript

我想使用文本框和按钮。当我在文本框中写入并单击按钮时,时间会下降,直到它变为00:00:00。然后页面重新加载。

1 个答案:

答案 0 :(得分:0)

据我所知,您希望倒计时从x下降到0,显示当前时间并在完成页面后重新加载?

以下是您可以做的事情:

_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);
}