<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Time</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;
}
clock();
</script>
</head>
<body>
<form>
<input type="text" id="clock" size="50" />
</form>
</body>
</html>
代码在上面,但除非我将clock()
替换为setInterval(clock, 1000)
,否则它无法正常工作并引发错误。这看起来很荒谬,为什么不能直接调用函数?