伦敦2012奥运会倒计时代码javascript版


JavaScript #倒计时2012-05-12 10:03

奥运会快到了,一个网http://yige.org给大家介绍个实用的代码。伦敦2012奥运会倒计时代码javascript版 。

<script type="text/javascript"> 
	var time_now_server,time_now_client,time_end,time_server_client,timerID;

	time_end=new Date("2012/07/28 04:12:00");

	time_end=time_end.getTime();

	time_now_server=new Date();//现在服务器的时间,为了测试我们用系统时间。更多的代码请查看 yige.org

	time_now_server=time_now_server.getTime();

	time_now_client=new Date();

	time_now_client=time_now_client.getTime();

	time_server_client=time_now_server-time_now_client;

	timerID = setTimeout("show_time()",1000);

	 

	function show_time()
	{

        var timer_d = document.getElementById("timerd"),
            timer_h = document.getElementById("timerh"),
            timer_m = document.getElementById("timerm"),
            timer_s = document.getElementById("timers");

            if(!timer_d){
                return ;
            }

        var time_now,time_distance;

        var int_day,int_hour,int_minute,int_second;

        var time_now=new Date();

            time_now=time_now.getTime()+time_server_client;

            time_distance=time_end-time_now;

            if(time_distance>0){

                int_day=Math.floor(time_distance/86400000)

                time_distance-=int_day*86400000;

                int_hour=Math.floor(time_distance/3600000)

                time_distance-=int_hour*3600000;

                int_minute=Math.floor(time_distance/60000)

                time_distance-=int_minute*60000;

                int_second=Math.floor(time_distance/1000)

                if(int_hour<10)

                int_hour="0"+int_hour;

                if(int_minute<10)

                int_minute="0"+int_minute;

                if(int_second<10)

                int_second="0"+int_second;


                timer_d.innerHTML=int_day;
                timer_h.innerHTML=int_hour;
                timer_m.innerHTML=int_minute;
                timer_s.innerHTML=int_second;

                timerID = setTimeout("show_time()",1000);

        }else{
                //timer.innerHTML =timer.innerHTML;
                clearTimeout(timerID)
        }

}

</script>
HTML使用:

<div class="aoyuntimer">
    <span id="timerd">
        76
    </span>
    <span id="timerh">
        18
    </span>
    <span>
        :
    </span>
    <span id="timerm">
        12
    </span>
    <span>
        :
    </span>
    <span id="timers">
        42
    </span>
</div>


相关文章

粤ICP备11097351号-1