JavaScript 实现倒计时 及 定时功能

1倒计时:

      

 countTime(value)

function countTime(value) {

var date = new Date();
    var now = date.getTime();

    //设置截止时间
    var endDate = new Date("2020/5/17 23:59:59");
    var end = endDate.getTime();
    //时间差
    var differTime = end - now;
    //定义变量,h,m,s保存倒计时的时间
    var h, m, s;
    if (differTime >= 0) {
        h = Math.floor(differTime / 1000 / 60 / 60);
        m = Math.floor(differTime / 1000 / 60 % 60);
        s = Math.floor(differTime / 1000 % 60);
        h = h < 10 ? ("0" + h) : h;
        m = m < 10 ? ("0" + m) : m;
        s = s < 10 ? ("0" + s) : s;
        var timeDom = "倒计时:" + h + "小时" + m + "分" + s + "秒";
        $("#countDown .J_lhour").text(h);
        $("#countDown .J_lminute").text(m);
        $("#countDown .J_lsec").text(s);

        //递归调用函数延时器
        setTimeout(function () {
            countTime(value)
        }, 1000);
        
    } else {
        $("#countDown .J_lhour").text('00');
        $("#countDown .J_lminute").text('00');
        $("#countDown .J_lsec").text('00');

    }
}

2,定时到某一事件,然后执行某件事情:

  

function isEnd() {
    var target = Date.parse('2020/06/28 12:00:00');
    if(target < Date.now()) {
        return true;
    }
    return false;
}

 

你可能感兴趣的:(JavaScript)