当前使用ajax的时间不起作用
current time using ajax not working
我正在尝试在我的网页上实现一个动态的当前时间,显示小时、分钟和秒。我正在使用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>
很长一段时间以来,我一直在努力寻找错误,但没有成功。。。
发布的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中加载的正文:
<body onload="showCurrentTime()">
是多余的。选择一个(最好是JS文件中的那个)。
相关文章:
- Javascript设置日期不起作用,显示错误的时间
- 为什么这个代码不起作用?我花了很长时间试图弄清楚这一点
- AngularJs的$cookies在过期时间内不起作用
- mysql时间戳格式的Javascript regex不起作用
- 时间驱动触发器在 Google 电子表格中不起作用
- 通过jQuery在IE中计算时间戳 - 不起作用
- 清除间隔在 6 个时间设置后不起作用间隔
- 在javascript中使用PHP时间字符串后,倒计时不起作用
- YouTube V3 API——按持续时间过滤相关视频信息,不;似乎不起作用
- 选项“;daysOfWeekDisabled”;在引导程序日期时间选择器中不起作用
- Javascript 比较时间不起作用
- Javascript窗口/图像加载时间 - 这应该不起作用,但它确实有效
- 引导时间选择器不起作用
- 日期时间选取器不起作用
- 在javascript中匹配两个日期时间不起作用
- 多个ajax请求过了一段时间不起作用
- 使用Date.getSeconds()检查时间不起作用
- 当前使用ajax的时间不起作用
- Flot-绘制时间不起作用的图表
- GSAP 补间无法正常工作.设置持续时间不起作用