<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>定时器</title>
<script type="text/javascript">
var attime;
function clock(){
var time=new Date();
attime=time.getHours()+":"+time.getMinutes()+":"+time.getSeconds() ;
document.getElementById("clock").value = attime;
}
var int=setInterval(clock,2000) ;
</script>
</head>
<body>
<form>
<input type="text" id="clock" size="50" />
</form>
</body>
</html>
结果显示,先是显示:Sat Aug 04 2018 17:27:19 GMT+0800 (中国标准时间)
再显示:17:27:19
我的代码:这样也可以:setInterval("clock()",100);
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>定时器</title>
<script type="text/javascript">
var attime;
function clock(){
var time=new Date();
attime= time.getHours() + ":" + time.getMinutes() + ":" + time.getSeconds();
document.getElementById("clock").value = attime;
}
setInterval("clock()",100);
</script>
</head>
<body>
<form>
<input type="text" id="clock" size="50" />
</form>
</body>
</html>
我的代码:
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>定时器</title>
<script type="text/javascript">
var attime;
function clock(){
var time=new Date();
attime= time.getHours() + ":" + time.getMinutes() + ":" + time.getSeconds();
document.getElementById("clock").value = attime;
}
setInterval(clock,100);
</script>
</head>
<body>
<form>
<input type="text" id="clock" size="50" />
</form>
</body>
</html>
浏览器刷新后重新提交代码
<script type="text/javascript">
var attime;
function clock(){
var time=new Date();
var h = time.getHours();
var i = time.getMinutes();
var s = time.getSeconds();
var attime = check(h)+":"+check(i)+":"+check(s);
document.getElementById("clock").value = attime;
}
function check(s){
if(s<10){
return "0"+s;
}else{
return s;
}
}
setInterval("clock()",1000);
</script>
我把你的代码粘贴到我本地运行只显示时分秒格式