美文网首页
JavaScript显示当前时间

JavaScript显示当前时间

作者: Leophen | 来源:发表于2018-12-06 11:19 被阅读0次

(setInterval的运用)

实现效果:
在文本框内显示实时的时间


183616.jpg

附上代码:

<!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(代码,交互时间),调用时间函数
    setInterval(clock(),1000);
    
</script>
</head>
<body>
    
    <form>
        
        <input type="text" id="clock" size="50"  />
        
    </form>
    
</body>
</html>

相关文章

网友评论

      本文标题:JavaScript显示当前时间

      本文链接:https://www.haomeiwen.com/subject/kqincqtx.html