html 每秒自动更新时间
<script>
setInterval("loginP.innerHTML=new Date().toLocaleString()+' 星期'+'日一二三四五六'.charAt(new Date().getDay());",1000);
</script>
<span id="loginP"/>
代码使用方式
<%@ page pageEncoding="UTF-8" contentType="text/html; UTF-8" isELIgnored="false" %>
<html>
<script>
/*通过id选择显示的位置 每1秒更新一次 */
setInterval("loginP.innerHTML=new Date().toLocaleString()+' 星期'+'日一二三四五六'.charAt(new Date().getDay());",1000);
/*通过id选择显示的位置 没有写更新时间就是直接显示 */
setInterval("loginP.innerHTML=new Date().toLocaleString()+' 星期'+'日一二三四五六'.charAt(new Date().getDay());");
</script>
<body>
<h1>
<span id="loginP"></span>
</h1>
</body>
</html>
运行格式如下:
代码原理:
我也不知道哪一种方式比较好,如果有前端大佬可以在评论区留言说一下
是写两个方法好,还是写一个方法让它一直更新数据呢?