使用AJAX运行实时时钟

时间:2012-05-16 05:26:45

标签: javascript ajax scope

既然我帮助AJAX运行得很好,我在使用它运行时钟功能时遇到了问题....

时钟代码(位于头部):

<script type="text/javascript">

var ampm = "AM"; //Default
var message="";


function startTime()
{
var today = new Date(); //Number goes here
var h=today.getHours();
var m=today.getMinutes();
var s=today.getSeconds();
// add a zero in front of numbers<10
m=checkTime(m);
s=checkTime(s);
h=checkTime2(h);
document.getElementById('clocktxt').innerHTML=h+":"+m+":"+s+ " " +ampm + " " + message;
//t=setTimeout('startTime()',500);
}

function checkTime(i)
{
if (i<10)
  {
  i="0" + i;
message = "How long you gonna sit there?";
  }
return i;
}

function checkTime2(i)
{
if (i>12)
  {
  i=i-12;
ampm="PM";
  }
return i;
}


//setInterval(startTime,1000);

</script>

AJAX代码(文档底部):

<script type='text/javascript'>
function CheckForChange(){
    //alert("<?echo (count($listArray)) . ' and ' . count(file($filename_noformat))?>");
    //if (<?echo count($listArray)?> == <?echo count(explode("\n", $filename_noformat))?>){
        //setInterval("alert('Yup, it is 1')", 5000);

        //alert('Now it is changed');
    //}

    var ajaxReady = new XMLHttpRequest();
    ajaxReady.onreadystatechange = function(){
        if (ajaxReady.readyState == 4){
            //Get the data
            //document.getElementById('clocktxt').innerHTML = ajaxReady.responseText;
            //startTime();
            //alert("here");
            //alert(ajaxReady.responseText);
        }
    }
    ajaxReady.open("GET","ServerTime.php",true);
    ajaxReady.send(null);
}

setInterval(CheckForChange(), 1000);
setInterval(startTime(),1000);
</script>

我要做的是将ServerTime.php的输入传递给时钟,这是从Unix时代到毫秒的毫秒数,因此时钟由AJAX每秒更新一次,时钟函数运行时每秒一个新的起始值....在我意识到时钟甚至没有被调用之前,我曾经有时钟功能的参数。

你认为有什么不对?我猜它与时钟有关,时钟的调用者在两个不同的脚本标签中,但我想不出如何绕过它。出于某种原因,当我将AJAX部分移动到相同的脚本标记中时,没有任何反应。

对Kolink:我有这个

function getTheNow(){
TIMESTAMP = <?php echo time(); ?>000;
    offset = new Date().getTime()-TIMESTAMP;
    setInterval(function() {
        var now = new Date();
        now.setTime(now.getTime()-offset);
        // print out the time according to the variable `now`
//alert(now);
    },5000);
return now;
}


function startTime()
{
var now = getTheNow;
//alert(now);
var today = new Date(); //Number goes here
var h=today.getHours();
var m=today.getMinutes();
var s=today.getSeconds();
// add a zero in front of numbers<10
m=checkTime(m);
s=checkTime(s);
h=checkTime2(h);
document.getElementById('clocktxt').innerHTML=h+":"+m+":"+s+ " " +ampm + " " + message;
t=setTimeout('startTime()',500);
}

function checkTime(i)
{
if (i<10)
  {
  i="0" + i;
message = "How long you gonna sit there?";
  }
return i;
}

function checkTime2(i)
{
if (i>12)
  {
  i=i-12;
ampm="PM";
  }
return i;
}


setInterval(startTime,1000);

2 个答案:

答案 0 :(得分:3)

计算机时钟不是那么不准确,你必须每秒重新同步它们。每隔十分钟,甚至每小时尝试一次。

事实上,我只是完全放弃了同步。这样做要容易得多:

<script type="text/javascript">
    TIMESTAMP = <?php echo time(); ?>000;
    offset = new Date().getTime()-TIMESTAMP;
    setInterval(function() {
        var now = new Date();
        now.setTime(now.getTime()-offset);
        // print out the time according to the variable `now`
    },1000);
</script>

答案 1 :(得分:1)

JavaScript 101错误

setInterval(CheckForChange(), 1000);
setInterval(startTime(),1000);

您没有分配该功能,您正在调用/执行它们,并且应该设置从这些功能返回的内容。删除()以便引用函数。

setInterval(CheckForChange, 1000);
setInterval(startTime,1000);