简体   繁体   English

如何使用 JavaScript 或 jQuery 动态更改时间(纯文本)?

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

相关问题 如何使用纯Javascript(无JQuery)将文本主体动态拆分为两个偶数列? - How do I go about dynamically splitting a body of text into two even columns with plain Javascript (no JQuery)? 我该怎么改变 <p> 使用javascript在一些定义的时间延迟后动态标记数据? - How can I change <p> tag data dynamically after some defined time delay using javascript? 如何使用 Javascript 动态更改工具提示的文本? - How can I dynamically change the text of a tooltip with Javascript? 如何使用纯JavaScript动态隐藏表格单元格的内容? - How can I hide the table cells contents dynamically using plain javascript? 使用纯JavaScript动态隐藏表格单元格内容时,如何保护单元格边框? - How can I protect the cell borders when hiding the table cells contents dynamically using plain javascript? 如何使用JavaScript或jQuery将纯文本网站捕获为变量? - How to capture plain text site into a variable using JavaScript or jQuery? 粘贴纯文本时如何更改tiptap的行为? - How can I change the behavior of tiptap when pasting plain text? 如何使用javascript / jquery动态更改图像? - How to dynamically change an image using javascript/jquery? 使用jquery,如何选择动态创建的文本? - Using jquery, how can I select dynamically created text? 如何使用 jQuery 动态添加删除线文本 animation? - How can I add strikethrough text animation using jQuery dynamically?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM