Hallo habe hier mal ein kleinen Countdown geschrieben. Ist ein wenig getrickst und jQuery ist ein wenig uebertrieben, ich bin aber von der Situation ausgegangen, dass jQuery bereits eingebunden ist und im Projekt eingesetzt wird. In diesem Thread ist der Javascript code in der HTML Datei, jedoch wuerde ich generell empfehlen das ganze auszulagern.
<html>
<head>
<title>Countdown</title>
<script type="text/javascript" src="jquery-1.3.2.min.js"></script>
</head>
<script>
var milisec = 0; //Start Nachkommastelle
var sec = 10; // Start Vorkommastelle
var time = 100; //Repeater Time (100 laesst es wie ein Sekunden Countdown aussehen)
var ele = '#downloadButton'; //setzt das element, auf das alles angewendet wird
$(ele).val(10); //Wert fuer ele wird ersetzt mit diesem
//Mach den Countdown
function showCountdown()
{
//Wenn die Milisec 0 erreicht, dann setzt sie auf 9 und ziehe von der skunde eine ab
if ( milisec <= 0 )
{
milisec = 9;
sec = sec - 1;
}
// wenn die secunde bei unter 0 ist dann setzt milisec auf 0 und die secunde wieder auf 0
if ( sec <= -1)
{
milisec = 0;
sec = sec + 1;
//setzt den neuen Wert von ele auf Download
$(ele).val( "Download" );
//die disabled funktion wird rausgenommen, sodass man den button benutzen kann
$(ele).attr( "disabled", "" );
}
else
{
// wenn der countdown noch nicht bei -1 angekommen ist, dann verfahre wie ein countdown, ziehe 1 immer ab und
milisec = milisec - 1;
$(ele).val( sec+"."+milisec ); //schreib den neuen Wert in das ele
setTimeout("showCountdown()",time) // und fang das gleiche wieder von vorne an nach time
}
}
</script>
<body onload="showCountdown()">
<div id="cnt">
<h1>Countdown:</h1>
<form>
<input type="button" value="Download" name="download" id="downloadButton" disabled="disabled" />
</form>
</div>
</body>
</html>
Vielleicht kann es ja jemand gebrauchen, bzw. hilft es jemanden.
Verbesserungen und Kritik sind mehr als willkommen : )
If you were enjoying this post and if you keen on reading more interesting stuff then do not hesitate to complete the free sign up.
After the free registration you will gain access to all areas and you will be able to communicate with other artists from all over the world.
In addition you will benefit from our Photoshop and coding section as well as from our huge (hundreds of gigabytes) free resource section where you can find everything you will need to be a successful (web) designer/artist.
Sign up now and enjoy the advantages as a registered member.
(This website will be ad-free after a complete free sign up.)