Javascript 为什么Jquery在使用set minutes函数设置时间之后显示当前系统时间?
{“小时”:“06”,“分钟”:“19”,“秒”:“22”} 更新时间函数Javascript 为什么Jquery在使用set minutes函数设置时间之后显示当前系统时间?,javascript,php,jquery,html,ajax,Javascript,Php,Jquery,Html,Ajax,{“小时”:“06”,“分钟”:“19”,“秒”:“22”} 更新时间函数 函数updatelock() { //var currentTime=新日期(); var currentHours=ser_date.getHours(); var currentMinutes=ser_date.getMinutes(); var currentSeconds=ser_date.getSeconds(); //如果需要,用前导零填充分和秒 currentMinutes=(currentMinutes
函数updatelock()
{
//var currentTime=新日期();
var currentHours=ser_date.getHours();
var currentMinutes=ser_date.getMinutes();
var currentSeconds=ser_date.getSeconds();
//如果需要,用前导零填充分和秒
currentMinutes=(currentMinutes<10?“0”:“)+currentMinutes;
currentSeconds=(currentSeconds<10?“0”:“)+currentSeconds;
//根据需要选择“上午”或“下午”
var timeOfDay=(当前小时数<12)?“AM”:“PM”;
//如果需要,将小时数组件转换为12小时格式
currentHours=(currentHours>12)?currentHours-12:currentHours;
//将小时数分量“0”转换为“12”
当前小时数=(当前小时数==0)?12:当前小时数;
//编写要显示的字符串
var currentTimeString=currentHours+“:”+currentMinutes+“:“+currentSeconds+”+timeOfDay;
$(“#时钟”).html(currentTimeString);
}
从服务器获取时间
函数getClock\u服务器()
{
var seru_date=新日期();
//ser_小时=d.hours;ser_分钟=d.minutes,ser_秒=d.seconds
$.get('http://127.0.0.1/readytoupload/time.php?time,函数(d){seru date.setMinutes(d.minutes);seru date.setSeconds(d.seconds);seru date.setHours(d.hours);};
}
$(文档).ready(函数()
{
getClock_服务器();
setInterval('UpdateLock()',1000);
});
时间显示部分
第一个问题是您在
getClock\u服务器中将seru date
作为局部变量,而需要更新全局变量
此外,为了更新时间,我认为您需要获得当前时间与发出服务器请求的时间之间的差异
var系统时间、服务日期;
函数updatelock(){
变量时间=新日期(ser_日期+新日期().getTime()-systime)
//var currentTime=新日期();
var currentHours=time.getHours();
var currentMinutes=time.getMinutes();
var currentSeconds=time.getSeconds();
//如果需要,用前导零填充分和秒
currentMinutes=(currentMinutes<10?“0”:“)+currentMinutes;
currentSeconds=(currentSeconds<10?“0”:“)+currentSeconds;
//根据需要选择“上午”或“下午”
var timeOfDay=(当前小时数<12)?“AM”:“PM”;
//如果需要,将小时数组件转换为12小时格式
currentHours=(currentHours>12)?currentHours-12:currentHours;
//将小时数分量“0”转换为“12”
当前小时数=(当前小时数==0)?12:当前小时数;
//编写要显示的字符串
var currentTimeString=currentHours+“:”+currentMinutes+“:“+currentSeconds+”+timeOfDay;
$(“#时钟”).html(currentTimeString);
}
函数getClock_server(){
//$.get('http://127.0.0.1/readytoupload/time.php?time,函数(d){
setTimeout(function(){//超时用于模拟类似ajax的异步调用
变量d={
时间:10,,
分钟:0,
秒:25
}
var tmp=新日期();
tmp设定分钟数(d.minutes);
tmp.设置秒(d.秒);
tmp设定小时数(d.hours);
ser_date=tmp.getTime();
systime=新日期().getTime();
updatelock();
设置间隔(updateClock,1000);
});
}
$(文档).ready(函数(){
getClock_服务器();
});
<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>