页面每过1秒向后台发送一次异步请求,读取服务器上的时间之后response给页面.
前台页面代码:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>MyWeb</title>
<script type="text/JavaScript" src="js/jQuery-1.4.2.min.js"></script>
<script type="text/javascript">
$(function(){
setInterval(function(){
$.get("Default.aspx?action=time×tamp="+new Date().getTime(),function(data){
$("#time").html(data);
});
},1000);
})
</script>
</head>
<body>
<form id="form1" runat="server">
<div id="time">
</div>
</form>
</body>
</html>
后台代码:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
if (Request.QueryString["action"] == "time")
{
Response.Write(DateTime.Now.ToString());
Response.End();
}
}
}