今さらながらJavaScriptでカウンター(指定の日付までカウントダウン)

var cnt = 0;
function display(date,obj) {
 if (typeof(date) != typeof(new Date())) { date = new Date(); }
 var rest = date - new Date();
 var day = 24*60*60*1000;
 if (rest < 0) { rest = 0; }
 
 ddate = date;
 dobj = obj;
 
 var d = Math.floor(rest/day);
 var h = Math.floor((rest%day)/(60*60*1000));
 var m = Math.floor((rest%day)/(60*1000))%60;
 var s = Math.floor((rest%day)/1000)%60%60;
 var ds = (rest%day)%60%60%100;

 if (typeof(obj) == typeof(getElementByTagName('body'))) {
  obj.innerHTML = d+'日'+h+'時間'+m+'分'+s+'秒'+ds;
  timer =setTimeout('display(ddate,dobj)', 10);
 }
}

任意のところで

<script type="text/javascript"><!-- // --><![CDATA[
 display(new Date(2010,6,30),document.getElementById('disp'));
// ]]></script>

Leave a Reply