最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

datetime - updating javascript time every second - Stack Overflow

programmeradmin3浏览0评论

Basically I want to have a live clock, one that updates every second! I've looked around and couldn't find something that has worked. Here is what I have tried:

function doDate()
{
    var str = "";

    var days = new Array("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday");
    var months = new Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");

    var now = new Date();

    str += "Today is: " + days[now.getDay()] + ", " + now.getDate() + " " + months[now.getMonth()] + " " + now.getFullYear();
    var updateTime = function() { setTimeout("doDate()", 1000); }

    document.getElementById("todaysDate").innerHTML = str;
}

This does not seem to work! I presume there is something I have done right in here?

Basically I want to have a live clock, one that updates every second! I've looked around and couldn't find something that has worked. Here is what I have tried:

function doDate()
{
    var str = "";

    var days = new Array("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday");
    var months = new Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");

    var now = new Date();

    str += "Today is: " + days[now.getDay()] + ", " + now.getDate() + " " + months[now.getMonth()] + " " + now.getFullYear();
    var updateTime = function() { setTimeout("doDate()", 1000); }

    document.getElementById("todaysDate").innerHTML = str;
}

This does not seem to work! I presume there is something I have done right in here?

Share Improve this question asked Oct 27, 2014 at 9:13 James111James111 15.9k16 gold badges86 silver badges129 bronze badges 2
  • This code has no effect: var updateTime = function() { setTimeout("doDate()", 1000); } – hindmost Commented Oct 27, 2014 at 9:18
  • Ill add this as a comment, cause its more of a code-review issue than a part of the answer: Why not just use the Date object and parse it out as you wish to display it, instead of creating a bunch of arrays every time the timer ticks? – Jite Commented Oct 27, 2014 at 9:24
Add a comment  | 

5 Answers 5

Reset to default 8

Please modify your code as follow:-

function doDate()
{
    var str = "";

    var days = new Array("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday");
    var months = new Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");

    var now = new Date();

    str += "Today is: " + days[now.getDay()] + ", " + now.getDate() + " " + months[now.getMonth()] + " " + now.getFullYear() + " " + now.getHours() +":" + now.getMinutes() + ":" + now.getSeconds();
    document.getElementById("todaysDate").innerHTML = str;
}

setInterval(doDate, 1000);
<div id="todaysDate"></div>

This line of code has no effect:

var updateTime = function() { setTimeout("doDate()", 1000); }

You define function variable updateTime which make postponed invocation of doDate, but this variable is unused in your code.

Instead of this you have to call setTimeout immediately without wrapping it with function:

setTimeout(doDate, 1000);

following code might help you

function doDate()
{
    var str = "";
    var now = new Date();
    str = now.toDateString() +' '+now.toLocaleTimeString() ;
    document.getElementById("todaysDate").innerHTML = str;
}
setInterval(doDate, 1000);

function doDate() {
  let currentDate = document.getElementById('currentDate');
  let current = new Date();
  currentDate.innerHTML = `Current Date and Time : <b>${current}</b> `;
}

setInterval(doDate, 1000);
<div id="currentDate"></div>

Your setTimeout call is incorrect:

var updateTime = function() { setTimeout("doDate()", 1000); }

Should rather look like:

setTimeout(doDate, 1000);

When adding a callback, you should not call the function (and in this case, you should not add it as a string), add the function itself.
And the function you wrap it inside of is not needed, just call the setTimeout function right away.

发布评论

评论列表(0)

  1. 暂无评论