I have this code to show the time and some date information in spanish:
我有这个代码用西班牙语显示时间和一些日期信息:
var date = new Date();
var hour = date.getHours().toString();
var minutes = date.getMinutes().toString();
var day = date.getDate();
switch (date.getMonth()) {
case 0:
month = "Ene";
break;
case 1:
month = "Feb";
break;
case 2:
month = "Mar";
break;
case 3:
month = "Abr";
break;
case 4:
month = "May";
break;
case 5:
month = "Jun";
break;
case 6:
month = "Jul";
break;
case 7:
month = "Ago";
break;
case 8:
month = "Sep";
break;
case 9:
month = "Oct";
break;
case 10:
month = "Nov";
break;
case 11:
month = "Dic";
break;
}
if (hour.length == 1) {
hour = '0' + hour;
}
if (minutes.length == 1) {
minutes = '0' + minutes;
}
setInterval($('#hour').html('<b>' + hour + ':' + minutes + '<br>' + '<span class="day">' + day + ' ' + month + '</span></b>'), 60000);
When I load the document it works ok. The problem the time is never udpated..why?
当我加载文档时,它可以正常工作。问题的时间永远不会被破坏..为什么?
1 个解决方案
#1
2
As pointed by @Travis J, setTimeout
first parameter should be a function. (See the doc.)
正如@Travis J所指出的,setTimeout第一个参数应该是一个函数。 (见文件)
In your case, you also want this function to recompute the date everytime it is called.
在您的情况下,您还希望此函数在每次调用时重新计算日期。
So something like :
所以类似于:
// Define a function that will update the html from current date
function updateDate() {
var date = new Date();
var hour = date.getHours().toString();
var minutes = date.getMinutes().toString();
var day = date.getDate();
switch (date.getMonth()) {
case 0:
month = "Ene";
break;
...
}
if (hour.length == 1) {
hour = '0' + hour;
}
if (minutes.length == 1) {
minutes = '0' + minutes;
}
// Update the page content
$('#hour').html('<b>' + hour + ':' + minutes + '<br>' + '<span class="day">' + day + ' ' + month + '</span></b>'
}
// Call the function the first time (setInterval will not call it right away)
updateDate();
// Schedule the function to be called every minute after that
setInterval(updateDate, 60000);
#1
2
As pointed by @Travis J, setTimeout
first parameter should be a function. (See the doc.)
正如@Travis J所指出的,setTimeout第一个参数应该是一个函数。 (见文件)
In your case, you also want this function to recompute the date everytime it is called.
在您的情况下,您还希望此函数在每次调用时重新计算日期。
So something like :
所以类似于:
// Define a function that will update the html from current date
function updateDate() {
var date = new Date();
var hour = date.getHours().toString();
var minutes = date.getMinutes().toString();
var day = date.getDate();
switch (date.getMonth()) {
case 0:
month = "Ene";
break;
...
}
if (hour.length == 1) {
hour = '0' + hour;
}
if (minutes.length == 1) {
minutes = '0' + minutes;
}
// Update the page content
$('#hour').html('<b>' + hour + ':' + minutes + '<br>' + '<span class="day">' + day + ' ' + month + '</span></b>'
}
// Call the function the first time (setInterval will not call it right away)
updateDate();
// Schedule the function to be called every minute after that
setInterval(updateDate, 60000);