//<![CDATA[
var displayCurrentTime = new function() {
// Get values...
var sysHour = getHours(); // Get Hours
var curHour = 0; // Initialize current hour (12 hr format)
var morningEvening = "AM"; // Initialize AM/PM notation
if (sysHour < 13) { // If it's in the morning, set time as is.
curHour = sysHour;
morningEvening = "AM"
} else {
curHour = sysHour - 12; // If it's in the evening, subtract 12 from the value and use "PM"
morningEvening = "PM"
}
var curMins: getMinutes; // Get current minutes...
// Capture the ID of the notification bar div, and compose a string from the above values.
var notificationBar = document.getElementById("notificationBar");
var dateTimeString = curHour + ":" + curMins + " " + morningEvening;
// All that code above files into this fun stuff.
notificationBar.innerHTML = dateTimeString;
}
window.setInterval(function() {
displayCurrentTime();
}, 1000);
//]]>
我一直在阅读一些信息,我想创建一个简单的脚本来获取小时和分钟,进行一些计算以确定它是AM还是PM,从这些结果中创建一个字符串变量,然后将其放入内部特定DIV元素的元素。它每秒执行一次。
根据我所阅读的内容,我编写的大多数代码似乎都有意义。在开始时,我尝试使用displayCurrentTime(){}函数以及您在下面看到的内容(var displayCurrentTime = new function(){}),但似乎都不起作用。我无法在页面上显示我的文字。注意:div的ID就是这里的NotificationBar,
此代码中是否有没有意义的东西,或者这实际上是否值得发布完整的HTML?
答案 0 :(得分:2)
您不需要在新定义的函数前使用new
,也无需实例化类。
您几乎没有语法错误,例如var curMins : ...
而不是var curMins = ...
。
此外,您不能在getHours()
对象之外使用getMinutes()
和Date
方法:
var displayCurrentTime = function() {
// Get values...
var d = new Date();
var sysHour = d.getHours(); // Get Hours
var curHour = 0; // Initialize current hour (12 hr format)
var morningEvening = "AM"; // Initialize AM/PM notation
if (sysHour < 13) { // If it's in the morning, set time as is.
curHour = sysHour;
morningEvening = "AM"
} else {
curHour = sysHour - 12; // If it's in the evening, subtract 12 from the value and use "PM"
morningEvening = "PM"
}
var curMins = d.getMinutes(); // Get current minutes...
var curSecs = d.getSeconds(); //optionally get seconds too
// Capture the ID of the notification bar div, and compose a string from the above values.
var notificationBar = document.getElementById("notificationBar");
var dateTimeString = curHour + ":" + curMins + ":" + curSecs + " " + morningEvening;
// All that code above files into this fun stuff.
notificationBar.innerHTML = dateTimeString;
}
window.setInterval(function(){ displayCurrentTime(); }, 1000);
<div id='notificationBar'>
Time Here
</div>
请参见有关Date对象:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date
关于函数和匿名函数表达式:
也:
https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Howto
答案 1 :(得分:1)
您的函数中存在一些语法错误,我将一一介绍它们:
new
。只需将其放下=)var displayCurrentTime = function() {
:
中的json语法var minutes: getMinutes
将值分配给变量。您还忘记了执行该功能,因此请尝试var minutes = getMinutes();
getHours
和getMinutes
,但是如果没有,则可以直接从新的Date
对象调用它们:var date = new Date();
var sysHours = date.getHours();
var minutes = date.getMinutes();
//...
应该这样做!
其他:您可以跳过将displayCurrentTime
添加到setInterval
函数内部的另一个函数中:window.setInterval(displayCurrentTime, 1000);
var displayCurrentTime = function() {
var date = new Date(); // Get current date object
var sysHour = date.getHours(); // Get Hours
var curHour = 0; // Initialize current hour (12 hr format)
var morningEvening; // No need to initialize
if (sysHour < 13) { // If it's in the morning, set time as is.
curHour = sysHour;
morningEvening = "AM"
} else {
curHour = sysHour - 12; // If it's in the evening, subtract 12 from the value and use "PM"
morningEvening = "PM"
}
var curMins = date.getMinutes(); // Get Minutes
// Capture the ID of the notification bar div, and compose a string from the above values.
var notificationBar = document.getElementById("notificationBar");
var dateTimeString = curHour + ":" + curMins + " " + morningEvening;
// All that code above files into this fun stuff.
notificationBar.innerHTML = dateTimeString;
}
window.setInterval(displayCurrentTime, 1000);
<div id='notificationBar'></div>