手记

js倒计时

    <!--倒计时-->
    window.onload = clock;
    function clock() {
        //获取结束的时间戳
        var timestamp = $("#shijiancuo").attr("title");
        //根据时间戳生成的时间对象
        var stop = new Date(timestamp * 1000);
        //获取当前时间
        var today = new Date();
        //剩余时间
        var shenyu = stop.getTime() - today.getTime(),//倒计时毫秒数
            shengyuD = parseInt(shenyu / (60 * 60 * 24 * 1000)),//转换为天
            D = parseInt(shenyu) - parseInt(shengyuD * 60 * 60 * 24 * 1000),
            shengyuH = parseInt(D / (60 * 60 * 1000)),//转换为小时
            H = D - shengyuH * 60 * 60 * 1000,
            shengyuM = parseInt(H / (60 * 1000)),//转换为分钟
            M = H - shengyuM * 60 * 1000;

        shengyuS = parseInt((shenyu - shengyuD * 60 * 60 * 24 * 1000 - shengyuH * 60 * 60 * 1000 - shengyuM * 60 * 1000) / 1000);//转换为秒
        //小于 10 的时候在前面加 0
        if (shengyuS < 10) {
            shengyuS = "0" + shengyuS
        } else {
            shengyuS
        }
        if (shengyuH < 10) {
            shengyuH = "0" + shengyuH
        } else {
            shengyuH
        }
        if (shengyuM < 10) {
            shengyuM = "0" + shengyuM
        } else {
            shengyuM
        }
        if (shengyuD < 10) {
            shengyuD = "0" + shengyuD
        } else {
            shengyuD
        }
        document.getElementById("time1").innerHTML = shengyuD;
        document.getElementById("time2").innerHTML = shengyuH;
        document.getElementById("time3").innerHTML = shengyuM;
        document.getElementById("time4").innerHTML = shengyuS;
        setTimeout("clock()", 500);
    }
0人推荐
随时随地看视频
慕课网APP