当前时间使用ajax不工作

发布于 2024-11-17 01:02:44 字数 2617 浏览 0 评论 0原文

我试图在我的网页上实现动态的当前时间,显示小时、分钟和秒。我正在使用 Ajax,但这似乎不起作用。

另外,我正在使用 Symfony 框架。

在正文中,我得到了

<body onload="showCurrentTime()">

在此之前:

<script type="text/javascript" src="/js/curr_time.js"></script>

curr_time.js

//Once the document is loaded, execute the showCurrentTIme function
//This is the AJAX function that displays the current time on the screen.
$(document).ready(function(){ showCurrentTime(); });    

function showCurrentTime() {
    //Call the current time web service,
    //grab the time off the server, and apply it to the 
    //end-user client after waiting for the document to load

    $(document).ready(function() {

        //Make the call to the XML web service
        $.get("currentDateTime.php", function(currentDateTime) {
            //Format the time returned by the server
            var time = [ $("hour", currentDateTime).text(), 
            ":", 
            $("min", currentDateTime).text() ];

            //Determine how many milliseconds to will wait until 
            //the time needs to be refreshed again
            var refresh = [(60 - $("sec", currentDateTime).text()) * 1000 ];

            //Display the time on the end-user client
            $("#currentTime").html(time.join(''));

            //Set a timer so that the time on the end-user client updates 
            // in sync with the server time to display the true current time
            setTimeout('showCurrentTime()', refresh);
        });
    });
}

在同一文件夹中,我有 PHP 文件 currentDateTime.php

<?php
#Need to specify that this is an XML document in order for it to work with AJAX
header('Content-Type: text/xml');

#Set variables equal to each part of the date and time
$year = date("Y"); 
$mon = date("m");
$mday = date("d");
$hour = date("H");
$min = date("i");
$sec = date("s");

#Create the XML document of the current date and time
echo '<?xml version="1.0" encoding="UTF-8"?>' . "\n";
echo '<currentDateTime>' . "\n";
echo "\t" . '<year>' . $year . '</year>' . "\n";
echo "\t" . '<month>' . $mon . '</month>' . "\n";
echo "\t" . '<day>' . $mday . '</day>' . "\n";
echo "\t" . '<hour>' . $hour . '</hour>' . "\n";
echo "\t" . '<min>' . $min . '</min>' . "\n";
echo "\t" . '<sec>' . $sec . '</sec>' . "\n";
echo '</currentDateTime>' . "\n";
?>

在正文中,

<p id="currentTime">--:--</p>

我已经尝试查找错误很长时间了,但没有成功。 ..

I am trying to achieve a dynamic current time on my webpage, showing hour, minutes and seconds. I'm using Ajax but this doesn't seem to be working.

Also, I'm using the Symfony framework.

In the body, I got

<body onload="showCurrentTime()">

Before that:

<script type="text/javascript" src="/js/curr_time.js"></script>

curr_time.js

//Once the document is loaded, execute the showCurrentTIme function
//This is the AJAX function that displays the current time on the screen.
$(document).ready(function(){ showCurrentTime(); });    

function showCurrentTime() {
    //Call the current time web service,
    //grab the time off the server, and apply it to the 
    //end-user client after waiting for the document to load

    $(document).ready(function() {

        //Make the call to the XML web service
        $.get("currentDateTime.php", function(currentDateTime) {
            //Format the time returned by the server
            var time = [ $("hour", currentDateTime).text(), 
            ":", 
            $("min", currentDateTime).text() ];

            //Determine how many milliseconds to will wait until 
            //the time needs to be refreshed again
            var refresh = [(60 - $("sec", currentDateTime).text()) * 1000 ];

            //Display the time on the end-user client
            $("#currentTime").html(time.join(''));

            //Set a timer so that the time on the end-user client updates 
            // in sync with the server time to display the true current time
            setTimeout('showCurrentTime()', refresh);
        });
    });
}

In the same folder, I have the PHP file currentDateTime.php

<?php
#Need to specify that this is an XML document in order for it to work with AJAX
header('Content-Type: text/xml');

#Set variables equal to each part of the date and time
$year = date("Y"); 
$mon = date("m");
$mday = date("d");
$hour = date("H");
$min = date("i");
$sec = date("s");

#Create the XML document of the current date and time
echo '<?xml version="1.0" encoding="UTF-8"?>' . "\n";
echo '<currentDateTime>' . "\n";
echo "\t" . '<year>' . $year . '</year>' . "\n";
echo "\t" . '<month>' . $mon . '</month>' . "\n";
echo "\t" . '<day>' . $mday . '</day>' . "\n";
echo "\t" . '<hour>' . $hour . '</hour>' . "\n";
echo "\t" . '<min>' . $min . '</min>' . "\n";
echo "\t" . '<sec>' . $sec . '</sec>' . "\n";
echo '</currentDateTime>' . "\n";
?>

And in the body,

<p id="currentTime">--:--</p>

I've been trying to find the error for a long time now, but with no success...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

鸵鸟症 2024-11-24 01:02:44

您发布的 showCurrentTime() 函数只做一件事:设置一个 $(document).ready() 处理程序。您希望从文档就绪中调用它(您已经在这样做),而不是在函数内设置另一个处理程序。只需从该函数内部删除额外的 $(document).ready() 内容,如下所示:

function showCurrentTime() {

   $(document).ready(function() {  // DELETE THIS LINE

       //Make the call to the XML web service
       $.get("currentDateTime.php", function(currentDateTime) {
           // rest of your function goes here...
       });

   });  // DELETE THIS LINE
} 

我确信这是主要问题。

第二个问题是,当您创建 refresh 变量时,您将其指定为指向一个包含一个元素的数组,而不是一个数字。删除方括号。

另外,在 JS 文件中添加以下行:

$(document).ready(function(){ showCurrentTime(); });

HTML 中的正文加载:

是多余的。选择一个(最好是 JS 文件中的那个)。

Your showCurrentTime() function as posted does only one thing: set a $(document).ready() handler. You want to call it from document ready (which you are already doing), not set another handler inside your function. Just get rid of the extra $(document).ready() stuff from inside that function as follows:

function showCurrentTime() {

   $(document).ready(function() {  // DELETE THIS LINE

       //Make the call to the XML web service
       $.get("currentDateTime.php", function(currentDateTime) {
           // rest of your function goes here...
       });

   });  // DELETE THIS LINE
} 

I'm sure that's the main problem.

The second problem is that when you create your refresh variable you're assigning it to point to an array with one element rather than to a number. Remove the square brackets.

Also, having this line in your JS file:

$(document).ready(function(){ showCurrentTime(); });

and a body onload in your HTML:

<body onload="showCurrentTime()">

is redundant. Pick one (preferably the one in the JS file).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文