开发者

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.

This is the html code, using Firebug to inspect the page ...

<strong class="big" id="ct">Friday, 3 September 2010 at 8:17:21 AM</strong>

with that time value i开发者_如何学运维ncrementing each second.

I'm guessing some JavaScript updates the 'ct' element .. but I can't find the code to how that's done.


The javascript code on this page is obfuscated and that't probably why you can't find it. To achieve this you could use the setInterval function:

$(function() {
    setInterval(function() {
        $('#ct').html(new Date().toString());
    }, 1000);
});


You can also use setTimeout method as follows:

function updateTime() {
    var dtString = new Date().toString();
    $('#lblDate').html(dtString);
    setTimeout(updateTime,1000);
}
updateTime();

It's different than setInterval. setTimeout must be called again in each function call to make it work like setInterval.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜