javascript - 为什么 Jquery 在使用 set minutes 函数设置时间后显示当前系统时间?

标签 javascript php jquery html ajax

http://127.0.0.1/readytoupload/time.php?time

{"hours":"06","minutes":"19","seconds":"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;

Date Object

var ser_date = new Date();

Update Time Function

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);

 }

Fetching Time From Server

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>

Time Display section

<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>

关于javascript - 为什么 Jquery 在使用 set minutes 函数设置时间后显示当前系统时间?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32498120/

相关文章:

php - 通过更改为单个查询而不是将其放入循环中进行优化

jQuery点击功能改变CSS

javascript - 增强现实库支持 React Native?

PHP 将 string 转换为 int 似乎给了我 0

javascript - 脚本标记中何时需要 CDATA 部分?

jquery - 无法使用 JQuery 和 Ajax 访问 <form> 属性

javascript - 在 flex-box 中使用 javascript 管理图像

javascript - map 引擎 + 地理位置 Javascript API

php - MySQL 多对多表到表矩阵

php - 电子邮件跟踪脚本