0

既然帮助我让 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);
4

2 回答 2

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>
于 2012-05-16T05:32:44.540 回答
1

JavaScript 101 错误

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

您不是在分配函数,而是在调用/执行它们并说应该设置从这些函数返回的内容。删除 () 以便您引用函数。

setInterval(CheckForChange, 1000);
setInterval(startTime,1000);
于 2012-05-16T05:33:07.253 回答