日期:2014-05-18  浏览次数:20459 次

问一个AJAX的小问题
C# code

//在页面上动态显示时钟的代码如下: 存在的问题是每过一秒就刷新一次页面,很难看,请问:如何用AJAX实现无刷新??
<script type="text/javascript">
    setInterval("aa.innerHTML=new Date().toLocaleString()+' 星期'+'日一二三四五六'.charAt(new Date   ().getDay());",1000);
</script>
<td class="datebar" id="aa" align="left">
                     </td>



------解决方案--------------------
<script type="text/javascript" language="javascript">
function updateTime()
{
var label = document.getElementById('yourControlID');
if (label) {
var time = (new Date()).localeFormat("T");
label.innerHTML = time;
}
}
updateTime();
window.setInterval(updateTime, 1000);
</script>
在updatepanel里放置你的显示控件yourControlID
------解决方案--------------------
//当页面加载时,取得当前服务器时间,然后自己加
<script>
var serverNow= new Date(<%=DateAndTime.Now().ToString("yyyy,M,dd,hh,mm,ss")%>);
alert(serverNow );
function showTime(){
timestr=serverNow.toLocaleString();
timestr=timestr.substr(timestr.indexOf(":")-2);//取得时间部分
document.getElementById("time_area").innerHTML = timestr;
serverNow.setSeconds(serverNow.getSeconds()+1);
window.setTimeout( "showTime()", 1000 );
}
</script>

<div id="time_area">
</div>

<script> showTime();</script>
------解决方案--------------------
HTML code

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
 <HEAD>
  <TITLE> New Document </TITLE>
  <META NAME="Generator" CONTENT="EditPlus">
  <META NAME="Author" CONTENT="">
  <META NAME="Keywords" CONTENT="">
  <META NAME="Description" CONTENT="">
  <SCRIPT LANGUAGE="JavaScript">
  <!--
    function a()
    {
       var d=new Date();
       document.getElementById("txtDate").value=d.getSeconds();
       //1秒定时执行a脚本
           setTimeout(a,1000)
    }
    window.onload=a;  
  //-->
  </SCRIPT>
 </HEAD>
 <BODY>
    <INPUT TYPE="text" id="txtDate">
 </BODY>
</HTML>