日期:2014-05-18  浏览次数:20706 次

想要2个关于时间的代码...
第一个是展示时间...每秒变换...页面中的时间也跟着变换...

第二个是倒计时...比如说倒计时5分钟...每秒钟的变化也能体现.....

------解决方案--------------------
javascript中setTimeout和setInterval函数

setTimeout(表达式,延时时间) 
在执行时,是在载入后延迟指定时间后,去执行一次表达式,记住,次数是一次 

用setTimeout实现的自动变化显示随机数的效果: 

<html> 
<head> 
<script> 
window.onload=sett; 
function sett() 

document.body.innerHTML=Math.random(); 
setTimeout("sett()",500); 

</script> 
</head> 
<body> 
</body> 
</html> 



setInterval(表达式,交互时间) 
则不一样,它从载入后,每隔指定的时间就执行一次表达式 

用setInterval实现的自动变化显示随机数的效果: 

<html> 
<head> 
<script> 
function sett() 

document.body.innerHTML=Math.random(); 

setInterval("sett();", 500); 
</script> 
</script> 
</head> 
<body> 
</body> 
</html> 

------解决方案--------------------
lz 以后发帖子尽量不要写【要代码】之类的词,见到这词一般我都会把帖子移到非技术区,因为要东西和讨论技术是两回事

lz 的帖子可以写成【关于两个时间的设置问题】

o(∩_∩)o...


第一个好做,lz 自己应该能搞定

我把第二的贴个代码给你,你参考一下:

HTML code

<!doctype html public "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="gb2312">
<head>
<head>
<title> 倒计时效果 </title>
<meta http-equiv="content-type" content="text/html; charset=gb2312" />
<meta name="keywords" content="" />
<meta name="description" content="" />
<script language="JavaScript">
<!-- //
var startTime = new Date();
var EndTime=startTime.getTime()+100*60*1000;
function GetRTime(){
var NowTime = new Date();
var nMS =EndTime - NowTime.getTime();
//var nD =Math.floor(nMS/(1000 * 60 * 60 * 24));
var nH=Math.floor(nMS/(1000*60*60)) % 24;
var nM=Math.floor(nMS/(1000*60)) % 60;
var nS=Math.floor(nMS/1000) % 60;
 //document.getElementById("RemainD").innerHTML=nD;
 document.getElementById("RemainH").innerHTML=nH;
 document.getElementById("RemainM").innerHTML=nM;
 document.getElementById("RemainS").innerHTML=nS;
if(nMS>5*59*1000&&nMS<=5*60*1000)
{
alert("还有最后五分钟!");
}
setTimeout("GetRTime()",1000);
}
window.onload=GetRTime;
// -->
</script>
</head>
<body>
<div id="CountMsg">倒计时还有:<strong id="RemainD"></strong><strong id="RemainH">XX</strong>时<strong id="RemainM">XX</strong>分<strong id="RemainS">XX</strong>秒</div>
</body>
</html>