简体   繁体   English

使用Javascript查询的时间因设备而异吗?

[英]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.

相关问题 javascript执行时间各不相同 - javascript execution time varies google lighthouse 如何计算 javascript 评估时间以及为什么它在不同环境中的相同脚本差异很大 - how google lighthouse calculates javascript evaluation time and why it varies by a huge margin for same scripts on different environments JavaScript 在两个设备上的显示时间不同(Apple-Windows) - JavaScript displayed time is different on two devices (Apple-Windows) 基于解析使用JavaScript为不同设备提供不同大小的图像 - Serving Different Size Images For Different Devices Based On Resolution Using JavaScript 为什么执行相同的JavaScript代码所需的时间会有所不同 - why the time taken to execute same javascript code varies 为不同的设备分离javascript - Separate javascript for different devices 使用Firefox时,在Android设备上将javascript日期转换为bst时间 - javascript dates converted to bst time on android devices when using Firefox 在不同时区的不同设备上的Unix时间 - Unix time on different devices in different time zones 在不同设备上调试CSS和JavaScript - Debugging css and javascript on different devices 适用于不同Android设备的JavaScript问题 - Problem with JavaScript for different Android devices
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM