Eğer belirlediğiniz tarihe doğru bir geri sayım yaptırmak istiyorsanız JavaScriptin zaman ve matematik methodlarını bilmeniz gerekir.
Basit bir geri sayım scripti yazalım. Bunun için ilk olarak sayfamız yüklenirken gerekli JavaScript fonksiyonunu çalıştırmamız gerekir
<body onLoad = "setInterval('gerisay()',1000)">
<div id="gerisay"></div>
onLoad ile gerısay fonksiyonumuz sayfa yüklenirken çalıştırılacak ve setInterval ile de her 1 saniye tekrar edilecektir. Geri sayma verisini yazdırmak içinde bir DIV oluşturmamız gerekir.
<script type="text/JavaScript">
Function countdown() {
newYear = new Date("January 1, 2007");
var today = new Date();
nextYear = today .getFullYear() + 1;
newYear.setFullYear(nextYear);
days = (newYear - today )/(1000*60*60*24);
var hours = (days - Math.floor(days))*24;
var minutes = (hours - Math.floor(hours))*60;
var seconds = (minutes - Math.floor(minutes))*60;
Document.getElementById('countdown').innerHTML= ' Yeni yıla ' + Math.floor(days) + ' gün, ' + Math.floor(hours) + ' saat, ' + Math.floor(minutes) + ' dakika, ' + Math.floor(seconds) + ' saniye';
}
</script>
Ilk olarak geri sayacağımız zamanı belirtmemiz gerekir. Ben burda yeni yıla geri saydıracağım. Daha sonra günümüz tarihi alırız ve burdaki yıla bir ekleriz. Böylece bir sonraki yeni yıl yılını almış oluruz. Daha sonra matematik işlemleri ile ne kadar gün, saat, dakika ve saniye kaldığını hesplatıp oluşturduğum DIV tagi içine bunu yazdırırız.
Eğer normal bir saat yazmak istiyorsanız
<script type="text/JavaScript">
Function saat() {
var today = new Date();
thisDate = today .getDate();
thisMonth = today .getMonth()+1;
thisYear = today .getFullYear();
thisSecond = today .getSeconds();
thisMinute = today .getMinutes();
thisHour = today .getHours();
var ampm = (thisHour<12) ? " am " : " pm ";
thisHour = (thisHour > 12) ? thisHour-12 : thisHour;
thisHour = (thisHour == 0) ? 12 : thisHour;
thisMinute = thisMinute < 10 ? "0" + thisMinute : thisMinute;
thisSecond = thisSecond < 10 ? "0" + thisSecond : thisSecond;
Document.getElementById('saat').innerHTML= thisMonth + "/" + thisDate + "/" + this year + ' ' + thisHour + ":" + thisMinute + ":" + thisSecond + ampm;
}
</script>
<body onLoad = "setInterval('saat()',1000)">
<div id="saat"></div>






