2016-10-29 2 views
-2

Ich möchte ein Textfeld und eine Schaltfläche verwenden. Wenn ich in das Textfeld schreibe und auf die Schaltfläche klicke, wird die Zeit bis 00:00:00 reduziert. Dann wird die Seite neu geladen.Countdown-Timer mit js

+0

Wenn der Eingang und Taste innerhalb eines ''

, vielleicht ... „[Wie verhindern Form von vorgelegt werden?] (Https: // Stackoverflow .com/questions/3350247/How-to-Prevent-Formular-from-being-submitted " –

+1

Bitte geben Sie weitere Informationen über Ihre Frage – Dennisrec

+1

Bitte beachten Sie die Hinweise in" [Wie stelle ich eine gute Frage?] (https : //stackoverflow.com/help/how-to-ask) "Es ist ziemlich schwierig für uns, mit dem, was wir nicht sehen können, Hilfe anzubieten. Bitte fügen Sie die relevantesten Codeschnipsel in Ihren Beitrag ein. –

Antwort

0

Soweit ich verstanden habe, möchten Sie einen Countdown von x bis 0 haben, die aktuelle Zeit anzeigen und die Seite neu laden, sobald sie fertig ist? Hier

ist das, was Sie tun können:

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