首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用ajax的当前时间不工作

使用ajax的当前时间不工作
EN

Stack Overflow用户
提问于 2011-06-24 02:38:04
回答 1查看 682关注 0票数 0

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

另外,我使用Symfony框架。

在身体里,我得到了

代码语言:javascript
复制
<body onload="showCurrentTime()">

在此之前:

代码语言:javascript
复制
<script type="text/javascript" src="/js/curr_time.js"></script>

curr_time.js

代码语言:javascript
复制
//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

代码语言:javascript
复制
<?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";
?>

在身体里,

代码语言:javascript
复制
<p id="currentTime">--:--</p>

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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2011-06-24 13:06:26

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

代码语言:javascript
复制
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中加载一个正文:

<body onload="showCurrentTime()">

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

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/6459014

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档