JSP通过AJAX获取服务端的时间,在页面上自动更新

1.在页面上引入js

<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB18030">
<title>Insert title here</title>
<script src="../scripts/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript">
	$(function() {
		setInterval(function() {
			$.get("getTime.jsp?timestamp=" + new Date().getTime(), function(
					data) {
				$("#time").html(data);
			});
		}, 1000);
	})
</script>
</head>
<body>
	<div id="time"></div>
</body>

2. 后台getTime.jsp代码

    <%
        Date date = new Date();
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        out.print(df.format(date));
    %>

如果仅仅显示时间,可单纯通过JavaScript获取本机的时间,自动更新。

posted @ 2019-04-06 15:54  zhouhb  阅读(426)  评论(0编辑  收藏  举报