为什么Jquery在使用设置分钟功能设置时间后显示当前系统时间
Why Jquery displays current system time after setting time using set minutes function?
http://127.0.0.1/readytoupload/time.php?time
{"小时":"06","分钟":"19","秒":"22"}
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script>
var ser_hours, ser_minutes, ser_seconds;
日期对象
var ser_date = new Date();
更新时间函数
function updateClock ( )
{
//var currentTime = new Date ( );
var currentHours = ser_date.getHours ( );
var currentMinutes = ser_date.getMinutes ( );
var currentSeconds = ser_date.getSeconds ( );
// Pad the minutes and seconds with leading zeros, if required
currentMinutes = ( currentMinutes < 10 ? "0" : "" ) + currentMinutes;
currentSeconds = ( currentSeconds < 10 ? "0" : "" ) + currentSeconds;
// Choose either "AM" or "PM" as appropriate
var timeOfDay = ( currentHours < 12 ) ? "AM" : "PM";
// Convert the hours component to 12-hour format if needed
currentHours = ( currentHours > 12 ) ? currentHours - 12 : currentHours;
// Convert an hours component of "0" to "12"
currentHours = ( currentHours == 0 ) ? 12 : currentHours;
// Compose the string for display
var currentTimeString = currentHours + ":" + currentMinutes + ":" + currentSeconds + " " + timeOfDay;
$("#clock").html(currentTimeString);
}
从服务器获取时间
function getClock_server ()
{
var ser_date = new Date();
//ser_hours = d.hours; ser_minutes = d.minutes, ser_seconds = d.seconds
$.get('http://127.0.0.1/readytoupload/time.php?time', function (d) { ser_date.setMinutes(d.minutes); ser_date.setSeconds(d.seconds); ser_date.setHours(d.hours);});
}
$(document).ready(function()
{
getClock_server();
setInterval('updateClock()', 1000);
});
</script>
<head>
<body>
时间显示部分
<div id="clock"></div>
</body>
</html>
第一个问题是将ser_date
作为getClock_server
中的局部变量,而需要更新全局变量。
此外,为了更新时间,我认为您需要获得当前时间和发出服务器请求的时间之间的差异
var systime, ser_date;
function updateClock() {
var time = new Date(ser_date + new Date().getTime() - systime)
//var currentTime = new Date ( );
var currentHours = time.getHours();
var currentMinutes = time.getMinutes();
var currentSeconds = time.getSeconds();
// Pad the minutes and seconds with leading zeros, if required
currentMinutes = (currentMinutes < 10 ? "0" : "") + currentMinutes;
currentSeconds = (currentSeconds < 10 ? "0" : "") + currentSeconds;
// Choose either "AM" or "PM" as appropriate
var timeOfDay = (currentHours < 12) ? "AM" : "PM";
// Convert the hours component to 12-hour format if needed
currentHours = (currentHours > 12) ? currentHours - 12 : currentHours;
// Convert an hours component of "0" to "12"
currentHours = (currentHours == 0) ? 12 : currentHours;
// Compose the string for display
var currentTimeString = currentHours + ":" + currentMinutes + ":" + currentSeconds + " " + timeOfDay;
$("#clock").html(currentTimeString);
}
function getClock_server() {
// $.get('http://127.0.0.1/readytoupload/time.php?time', function(d) {
setTimeout(function() { //the timeout is used to simulate a ajax like async call
var d = {
hours: 10,
minutes: 0,
seconds: 25
}
var tmp = new Date();
tmp.setMinutes(d.minutes);
tmp.setSeconds(d.seconds);
tmp.setHours(d.hours);
ser_date = tmp.getTime();
systime = new Date().getTime();
updateClock();
setInterval(updateClock, 1000);
});
}
$(document).ready(function() {
getClock_server();
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<div id="clock"></div>
相关文章:
- 更改默认时区后,如何在本地时间设置时刻格式
- 使用HTML5 Navigation Timing API时,导航开始时间设置为0
- 具有完整时间设置的嵌套 JQuery 动画
- 清除间隔在 6 个时间设置后不起作用间隔
- JQuery 日期选择器 - 根据当前时间设置 MinDate
- 通过Javascript错误将考试时间设置为30分钟
- 如何将东部标准时间设置为日期选择器 (jquery ui) - 使用 javascript
- 如何在javascript中将当前时间设置为其他时间
- 使用JS根据时间设置列的背景颜色
- 如何将一些指定的时间设置为ajax调用,这样如果请求正在花费时间检索数据,它应该来到FAIL块
- 如何将时间设置为定期?JAVA
- 根据一天中的时间设置默认选项的选定值
- 将默认时间设置为mobisroll时间选择器
- 如何在正确的时间设置JS事件
- 客户端时间设置在javascript中破坏了可读日期
- jQuery Timepicker和jQuery Datepair:如何将结束时间设置为15分钟
- Bootstrap DateTimePicker将时间设置为午夜
- 如何将这种日期时间设置为“2013-03-12T22:15:40+08:00”到“12 Mar 2012 02:15:4
- 使用php服务器时间设置Javascript时间
- 如何实现将新的Firebase自定义身份验证最大过期时间设置为1小时的用例