[英]How can I dynamically change the time (which is plain text) using JavaScript or jQuery?
On this website the textual time is dynamically updated, I'm guessing using JavaScript.在这个网站上,文本时间是动态更新的,我猜是使用 JavaScript。
This is the html code, using Firebug to inspect the page ...这是html代码,使用Firebug检查页面...
<strong class="big" id="ct">Friday, 3 September 2010 at 8:17:21 AM</strong>
with that time value incrementing each second.该时间值每秒递增。
I'm guessing some JavaScript updates the 'ct' element .. but I can't find the code to how that's done.我猜一些 JavaScript 更新了 'ct' 元素 .. 但我找不到如何完成的代码。
The javascript code on this page is obfuscated and that't probably why you can't find it.此页面上的 javascript 代码已被混淆,这可能不是您找不到它的原因。 To achieve this you could use the
setInterval
function:为此,您可以使用
setInterval
函数:
$(function() {
setInterval(function() {
$('#ct').html(new Date().toString());
}, 1000);
});
You can also use setTimeout method as follows:您还可以使用 setTimeout 方法,如下所示:
function updateTime() {
var dtString = new Date().toString();
$('#lblDate').html(dtString);
setTimeout(updateTime,1000);
}
updateTime();
It's different than setInterval.它与 setInterval 不同。 setTimeout must be called again in each function call to make it work like setInterval.
setTimeout 必须在每次函数调用中再次调用,以使其像 setInterval 一样工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.