js 倒计时

以下是做一个倒计时

倒计时start

<input type="button" onclick="getCode();" class="dialog_cxhq_code" value="重新获取" />

js代码:

var divObj = $(".dialog_cxhq_code");
time(divObj);

var wait=60;
function time(o){
	if (wait == 0){
		$('.dialog_cxhq_code').removeAttr("disabled");
//		$(".dialog_cxhq_code").attr("disabled", false);
		$(".dialog_cxhq_code").val("重新获取");
		wait = 60;
	} else {
		$(".dialog_cxhq_code").attr("disabled", true);
		$(".dialog_cxhq_code").val("(" + wait + ")重新获取");
		wait--;
		setTimeout(function(){
		time(o)
	},1000)
	}
}

  最后大致的功能是这样的,当然样式我木有贴出来

倒计时end/************************/

还有一个值得收藏的是时间差计算,发布几天前或者发布几分钟计算:

    /**时间差计算
    **/
    function getDateDiff(dataTime){
        var result="";
        var oldTime = new Date(Date.parse(dataTime.replace(/-/g,"/"))).getTime(); 
        var minute = 1000 * 60;
        var hour = minute * 60;
        var day = hour * 24;
        var halfamonth = day * 15;
        var month = day * 30;
        var now = new Date().getTime();
        var diffValue = now - oldTime;
        if(diffValue < 0){
         return result;
         }
        var monthC =diffValue/month;
        var weekC =diffValue/(7*day);
        var dayC =diffValue/day;
        var hourC =diffValue/hour;
        var minC =diffValue/minute;
                if(monthC>=1){
            result="发表于" + parseInt(monthC) + "个月前";
         }else if(weekC>=1){
            result="发表于" + parseInt(weekC) + "周前";
         }else if(dayC>=1){
            result="发表于"+ parseInt(dayC) +"天前";
                }else if(dayC >= 1){
            result = "昨天"+dataTime.substr(10,6);
         }else if(dayC >=2){
             result = "前天"+dataTime.substr(10,6);
         }else if(hourC>=1){
            result = parseInt(hourC) +"小时前";
         }else if(minC>=1){
            result = parseInt(minC) +"分钟前";
         }else{
            result="刚刚发表";
         }
        return result;
    }            

 使用:getDateDiff('2016-03-30 22:17:01');

 

 

请无视这句话 个人收藏而已:data.consultation[i].zixun.newTime=newTime;

posted @ 2016-04-01 15:42  小安妮的熊  Views(152)  Comments(0Edit  收藏  举报