javascript实现倒计时

时间:2022-01-03 22:04:22
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>js实现倒计时</title>
</head>
<body>
<div>
    <span id="t_d"></span>
    <span id="t_h"></span>
    <span id="t_m"></span>
    <span id="t_s"></span>
</div>
<script type="text/javascript">
    function display(year,month,day,hour,minute,second){
        var endTime=new Date(year,month-1,day,hour,minute,second);
        var nowTime=new Date();
        var time=endTime.getTime()-nowTime.getTime();
        var d=0;
        var h=0;
        var m=0;
        var s=0;
        if(time>=0){
            d=Math.floor(time/1000/60/60/24);
            h=Math.floor(time/1000/60/60%24);
            m=Math.floor(time/1000/60%60);
            s=Math.floor(time/1000%60);
            document.getElementById("t_d").innerHTML=d+"天";
            document.getElementById("t_h").innerHTML=h+"时";
            document.getElementById("t_m").innerHTML=m+"分";
            document.getElementById("t_s").innerHTML=s+"秒";
        --s;
        }
    }
    setInterval("display(2017,9,1,15,10,00)",1000);

</script>

</body>
</html>