javascript 活动倒计时

var nowtime = new Date(<?php echo time();?>*1000).getTime();
function liftTime(end, txt){
	if(end > 0) {
		
		var endtime = new Date(end).getTime();
		
		var youtime = endtime-nowtime;

		
		var seconds = youtime/1000;
		var minutes = Math.floor(seconds/60);
		var hours = Math.floor(minutes/60);
		var days = Math.floor(hours/24);
		var CHour= hours % 24;
		var CMinute= minutes % 60;
		var CSecond= Math.floor(seconds%60);

		if(endtime <= nowtime){
			window.location.reload(); 
		}
		else {
			$('#tstr').html("<em>"+txt+"</em><span>"+formetNum(days)+"</span><em>天</em><span>"+formetNum(CHour)+"</span><em>时</em><span>"+formetNum(CMinute)+"</span><em>分</em><span>"+formetNum(CSecond)+"</span><em>秒</em>");
			
			nowtime += 1000;
			setTimeout("liftTime("+end+", '"+txt+"')",1000);
		}
		
	}
	else {
		$('#tstr').html("<em>"+txt+"</em><span>00</span><em>天</em><span>00</span><em>时</em><span>00</span><em>分</em><span>00</span><em>秒</em>");
	}
	
}

function formetNum(num) {
	if(num < 10) {
		return "0"+num;
	}
	else {
		return num;
	}
}

/**
 * 页面初始化
 */
$(document).ready(function(){
	<?php 
		$time = time();
		if($time < $row['star_date']) {
			?>liftTime(<?php echo $row['star_date']?>*1000,"距开始还剩");<?php 
		}
		else if($time < $row['end_date']) {
			?>liftTime(<?php echo $row['end_date']?>*1000,"距结束还剩");<?php
		}
		else {
			?>liftTime(0,"活动已结束");<?php
		}
	?>
});

你可能感兴趣的:(javascript 活动倒计时)