JSP經過AJAX獲取服務端的時間,在頁面上自動更新

1.在頁面上引入jsjavascript

<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代碼html

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

若是僅僅顯示時間,可單純經過JavaScript獲取本機的時間,自動更新。java

相關文章
相關標籤/搜索