js 倒计时

1. 60s 倒计时

var clicking;//防止重复点击

function taketime(obj) {
    var timeInterval; //定时任务

    if (clicking) {
        return;
    }

    var number = 60;
    timeInterval= window.setInterval(function () {
        if (number <= 0) {
            obj.html("发送验证码");
            window.clearInterval(timeInterval);
            clicking = false;
        } else {
            clicking = true;
            obj.html(number -= 1).append(" s后重发");//.......每秒内容更改
        }
    }, 1000);
}

 

2. 天数、时、分、秒


//html
 <body onload="leftTimer()">
//js
function leftTimer(year,month,day,hour,minute,second){
var leftTime = (new Date(year,month-1,day,hour,minute,second)) - (new Date()); //计算剩余的毫秒数 var days = parseInt(leftTime / 1000 / 60 / 60 / 24 , 10); //计算剩余的天数 var hours = parseInt(leftTime / 1000 / 60 / 60 % 24 , 10); //计算剩余的小时 var minutes = parseInt(leftTime / 1000 / 60 % 60, 10);//计算剩余的分钟 var seconds = parseInt(leftTime / 1000 % 60, 10);//计算剩余的秒数 days = checkTime(days); hours = checkTime(hours); minutes = checkTime(minutes); seconds = checkTime(seconds); setInterval("leftTimer(2016,11,11,11,11,11)",1000); document.getElementById("timer").innerHTML = days+"" + hours+"小时" + minutes+""+seconds+""; } function checkTime(i){ //将0-9的数字前面加上0,例1变为01 if(i<10) { i = "0" + i; } return i; }

 

posted @ 2016-12-28 15:12  NewLife365  阅读(171)  评论(0编辑  收藏  举报