[英]Time queried using Javascript varies with Different Devices?
I am trying to create a count-down timer using mysql, php and javascript. 我正在尝试使用mysql,php和javascript创建一个倒数计时器。 I am trying to make the ending-time a constant and stored in mysql server, then it is queried every second from javascript via php.
我试图使结束时间为常数并存储在mysql服务器中,然后每秒通过php从javascript查询一次。 Then, I am getting the current time in javascript, every second using new Date() function.
然后,我使用新的Date()函数每秒获取一次javascript中的当前时间。 subtracting the current time from ending-time gives me the time left.
从结束时间中减去当前时间可得到剩余时间。
However, my problem is that when the website is viewed in different devices, the current time queried using javascript is varied by seconds and sometimes even minutes... Please help me solve this problem.. thanks in advance. 但是,我的问题是,在不同的设备上浏览网站时,使用javascript查询的当前时间会变化几秒钟,有时甚至是几分钟...请提前帮助我解决此问题。
Here's what I have tried. 这是我尝试过的。
setInterval(function()
{
$.post('QueryTime.php', 'get=true', function(data,status)
{
if(status)
var endingtime = data;
else
alert("Server Error! Please refresh the page.");
});
endingtime = parseInt(endingtime);
var now = new Date();
now = now.getTime();
var timeleft = (date - now)/1000;
},1000);
Do you mean the actual local time of the devices varies a bit? 您是说设备的实际本地时间会有所不同吗? This is usually the case, more devices mean more times :) What you may want is to not get the time necessarily via
new Date().getTime()
every time but maybe to get the current time from the same remote location for every device. 通常是这样,更多的设备意味着更多的时间:)您可能想要的是不必每次都通过
new Date().getTime()
获得时间,而是可能从每个设备的相同远程位置获取当前时间。 。
This remote location can be your server, which holds the ending time as i understand or something from the internet: 这个远程位置可以是您的服务器,根据我的理解,它可以保存结束时间,也可以来自互联网
Here's some code to get the Unix timestamp via an HTTP request in Javascript: 这是一些通过Javascript中的HTTP请求获取Unix时间戳的代码:
var xmlHttp = new XMLHttpRequest(); xmlHttp.open("GET", "http://currentmillis.com/api/millis-since-unix-epoch.php", false); xmlHttp.send(null); document.getElementById('info').innerHTML = xmlHttp.responseText;
<div id='info'></div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.