js 倒计时(可自定义时间)

原文 链接 http://www.cnblogs.com/liygheart/p/3605160.html

<html>
    <head>
        <title>js 倒计时</title>
    </head>
    <body>
        <div>
            <h4>距离<input type="text" id="inputdate" value="2014-4-5"/> <button onclick="djs()">还有</button></h4>
            <div id="left"> </div>
        </div>
    </body>
</html>
<script LANGUAGE="javascript">
    djs();
    var timerID = null;
    var timerRunning = false;
    var year, month, day;
    function djs () {
        var inputdate = document.getElementById("inputdate").value;
        var splitdate = inputdate.split("-");
        year = splitdate[0];
        month = splitdate[1];
        day = splitdate[2];
        startclock();
    }
    function showtime() {
        Today = new Date();
        var NowHour = Today.getHours();
        var NowMinute = Today.getMinutes();
        var NowMonth = Today.getMonth();
        var NowDate = Today.getDate();
        var NowYear = Today.getYear();
        var NowSecond = Today.getSeconds();
        NowYear=1900+NowYear;
        Today = null;
        Hourleft = 23 - NowHour
        Minuteleft = 59 - NowMinute
        Secondleft = 59 - NowSecond
        Yearleft = year - NowYear
        Monthleft = month - NowMonth - 1
        Dateleft = day - NowDate
        if (Secondleft<0){
            Secondleft=60+Secondleft;
            Minuteleft=Minuteleft-1;
        }
        if (Minuteleft<0){
            Minuteleft=60+Minuteleft;
            Hourleft=Hourleft-1;
        }
        if (Hourleft<0){
            Hourleft=24+Hourleft;
            Dateleft=Dateleft-1;
        }
        if (Dateleft<0){
            Dateleft=31+Dateleft;
            Monthleft=Monthleft-1;
        }
        if (Monthleft<0){
            Monthleft=12+Monthleft;
            Yearleft=Yearleft-1;
        }
        Temp=Yearleft+'年-'+Monthleft+'月-'+Dateleft+'天 '+Hourleft+'时:'+Minuteleft+'分:'+Secondleft+'秒'
        document.getElementById("left").innerHTML=Temp;
        timerID = setTimeout("showtime()",1000);
        timerRunning = true;
    }
    var timerID = null;
    var timerRunning = false;
    function stopclock () {
        if(timerRunning){
            clearTimeout(timerID);
            timerRunning = false;
        }
    }
    function startclock () {
        stopclock();
        showtime();
    }
</script>

 

运行后效果:

posted @ 2016-06-29 17:57  恋恋西风  阅读(493)  评论(0编辑  收藏  举报