javascript 显示剩余的时间

function formatTime(reallyTime){
		var time = "";
		var showTime = reallyTime;
		if(showTime>=3600){
			time = time+1+":";
			showTime = showTime-3600;
		}else{
			time = time+0+":";
		}
		if(showTime>=60 && showTime <3600){
				var tmp=parseInt(showTime/60)
				var minute =""+tmp;
				if(tmp<10){
					minute = "0"+tmp;
				}
				time = time+minute+":";
				showTime = showTime-tmp*60;
			}else{
				time = time+"00"+":";
			}
		if(showTime<60){
			if(showTime<10){
				showTime = "0"+showTime;
			}
			time = time+showTime;
		}
		return time;
	}
	
	function showTime(showTimeFlag){
		var showTime=parseInt(showTimeFlag);
//showTimeDiv是一个div的id
		document.getElementById("showTimeDiv").innerHTML="考试剩余时间:"+formatTime(showTime);
		if(showTimeFlag<=0){
			return false;
		}
		showTime = showTime -1;
		setTimeout("showTime("+showTime+")",999.999);
	}
		showTime(4800);


var endTime = new Date(2008,07,20,14,50,00);//2008年8月20日 11:50:00
	//注意:endTime > now才可以
	
	var now = new Date();
	var showtime=endTime.getTime()- now.getTime();
	//alert(endTime.getTime()- now.getTime());
	//用于显示考试的剩余时间
	function formatTime(reallyTime){
		var time = "";
		var showTime = reallyTime;
		if(showTime>=3600){
			time = time+1+":";
			showTime = showTime-3600;
		}else{
			time = time+0+":";
		}
		if(showTime>=60 && showTime <3600){
				var tmp=parseInt(showTime/60)
				var minute =""+tmp;
				if(tmp<10){
					minute = "0"+tmp;
				}
				time = time+minute+":";
				showTime = showTime-tmp*60;
			}else{
				time = time+"00"+":";
			}
		if(showTime<60){
			if(showTime<10){
				showTime = "0"+showTime;
			}
			time = time+showTime;
		}
		return time;
	}
	
	function showTime(showTimeFlag){
		var showTime=parseInt(showTimeFlag);
		document.getElementById("showTimeDiv").innerHTML="考试剩余时间:"+formatTime(showTime);
		if(showTimeFlag<=0){
			return false;
		}
		showTime = showTime -1;
		setTimeout("showTime("+showTime+")",999.999);
	}
	showTime(showtime/1000);

你可能感兴趣的:(JavaScript)