Javascript 实时显示

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5704378/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 18:31:49  来源:igfitidea点击:

Displaying real time

phpjavascriptjquery

提问by Zilarion

I am currently displaying date/time on my webpage using the date function PHP provides. However, using this function, the date/time will only be updated when reloading the page. I wish to have the date/time updated every second instead.

我目前正在使用 PHP 提供的日期函数在我的网页上显示日期/时间。但是,使用此功能,日期/时间只会在重新加载页面时更新。我希望每秒更新一次日期/时间。

I assume I have to use either javascript or jQuery/ajax for this, however I have no clue on how to do this. I was hoping anyone here could give me some advice.

我假设我必须为此使用 javascript 或 jQuery/ajax,但是我不知道如何执行此操作。我希望这里的任何人都可以给我一些建议。

Thanks in advance.

提前致谢。

采纳答案by Alnitak

Here's what I use (using jQuery)

这是我使用的(使用 jQuery)

var updateClock = function() {
    function pad(n) {
        return (n < 10) ? '0' + n : n;
    }

    var now = new Date();
    var s = pad(now.getUTCHours()) + ':' +
            pad(now.getUTCMinutes()) + ':' +
            pad(now.getUTCSeconds());

    $('#clock').html(s);

    var delay = 1000 - (now % 1000);
    setTimeout(updateClock, delay);
};

This is more accurate than just having a 1000ms timer since otherwise you get drift in the timings.

这比仅使用 1000 毫秒计时器更准确,否则您会在计时中出现偏差。

回答by Yoann

I suget you to use the Date javascript oblect for display real time date/time

我建议您使用 Date javascript oblect 来显示实时日期/时间

function Timer() {
   var dt=new Date()
   document.getElementById('time').innerHTML=dt.getHours()+":"+dt.getMinutes()+":"+dt.getSeconds();
   setTimeout("Timer()",1000);
}
Timer();

回答by Aaron W.

Are you looking to show the client's time or the server's time? You can look into javascript to have a running clock on your webpage, but it will just show the user's computer time. PHP will show the server's clock.

您是要显示客户端的时间还是服务器的时间?您可以查看 javascript 以在您的网页上运行时钟,但它只会显示用户的计算机时间。PHP 将显示服务器的时钟。

http://www.elated.com/res/File/articles/development/javascript/creating-a-javascript-clock/clock.html

http://www.elated.com/res/File/articles/development/javascript/creating-a-javascript-clock/clock.html

Or the jQuery plugin: http://plugins.jquery.com/project/jqClock

或者 jQuery 插件:http: //plugins.jquery.com/project/jqClock

回答by Craig Sefton

You would need to use JavaScript/jQuery.

您将需要使用 JavaScript/jQuery。

I wouldn't advise using Ajax and calls to the Webserver to load the time, as that would be an HTTP request every second for every user, which is a heck of a lot of traffic.

我不建议使用 Ajax 并调用 Web 服务器来加载时间,因为这将是每个用户每秒发送一次 HTTP 请求,这会带来大量流量。

A tutorial for doing it in Javascript is here: http://www.elated.com/articles/creating-a-javascript-clock/

在 Javascript 中执行此操作的教程在这里:http: //www.elated.com/articles/creating-a-javascript-clock/

(Search Google for "Javascript" clock, there are many examples).

(在谷歌搜索“Javascript”时钟,有很多例子)。

回答by Gowri

jquery ajax

jQuery ajax

setTimeout(function(){
    $.ajax({
      url: "server.php",
      type:"post",
      success: function(data){
       $('#showtime').html(data);

      }
    });
),1000});

server.php

服务器.php

update query//here you echo date/time in convenient format

更新查询// 在这里您以方便的格式回显日期/时间

html

html

<div id="showtime"></div>

ref http://api.jquery.com/jQuery.ajax/

参考 http://api.jquery.com/jQuery.ajax/

回答by RandomCoder

One way to do it which gives you the server time but without polling every second is to pass the timestamp to javascript (not the formatted time), then let javascript turn it into a formatted date and use setTimeout to add 1 second to the stamp, you just need to format the new timestamp each second. This gives you the server time but without expensive polling.

一种为您提供服务器时间但不每秒轮询的方法是将时间戳传递给 javascript(不是格式化时间),然后让 javascript 将其转换为格式化日期并使用 setTimeout 将 1 秒添加到标记中,您只需要每秒格式化新的时间戳。这为您提供了服务器时间,但无需昂贵的轮询。