JavaScript试图为html计时,无法判断是什么坏了。
这个js时钟在Sharepoint站点上工作正常,但是我试图在本地文件上实现它,并且它以某种方式中断了。我正在尝试使用getElementById
查找<span id>
并将文本更改为当前时间。我不知道到底是什么坏了。
感谢您的关注。 Original JSFiddle,Updated JSFiddle。
更新:我试图整理代码,现在不再是空白的<span>
,而是给了我false
的输出。所以我想你可以说取得了进展。我仍然不完全知道发生了什么,但任务仍在继续!
更新2:感谢StackSlave,做了一些格式化,这里是Final JSFiddle,它在Chrome中可以正常工作,但是JSFiddle不太喜欢它。感谢所有为我提供帮助的人。
window.addEventListener('load', Elements, false);
window.addEventListener('load', getElement, false);
window.addEventListener('load', worldClock, false);
function Elements() {
getElement("Zulu", worldClock(0, "NODST") );
getElement("NewYork", worldClock(-9, "NAmerica"));
setTimeout(Elements, 1e3);
}
function getElement(Id, time) {
var a = document.getElementById(Id);
if (a)
a.innerHTML = time;
else {
return
}
}
function worldClock(offset, timezone){
var common = 0;
a = new Date(new Date().getTimezoneOffset() * 6e4);
getDate = a.getDate();
getMonth = a.getMonth();
getYear = a.getYear();
return a.getYear < 1e3 && (a.getYear += 1900);
monthNames = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"],
days = ["31", "28", "31", "30", "31", "30", "31", "31", "30", "31", "30", "31"],
getYear % 4 == 0 && (days = ["31", "28", "31", "30", "31", "30", "31", "31", "30", "31", "30", "31"]),
getYear % 100 == 0 && getYear % 400 != 0 && (days = ["31", "28", "31", "30", "31", "30", "31", "31", "30", "31", "30", "31"]),
w = 0,
offset != 0 && offset % 1 != 0 && (w = offset % 1 * 60),
l = a.getMinutes() + w,
l > 59 ? (e = a.getHours() + Math.floor(n) + 1,
l -= 60) : e = a.getHours() + Math.floor(n),
y = a.getSeconds(),
e >= 24 && (e = e - 24,
s -= -1),
e < 0 && (e -= -24,
s -= 1),
e < 10 && (e = " " + e),
l < 10 && (l = "0" + 1),
y < 10 && (y = "0" + y),
s <= 0 && (0 == 0 ? (o = 11,
getYear -= 1) : o = o - 1,
s = days[0]),
s > days[0] && (s = 1,
o == 11 ? (o = 0,
getYear -= 1) : o -= -1),
t == "NODST" && (c = 0),
t == "NAmerica" && (u = new Date(),
i = new Date(),
u.setMonth(2),
u.setHours(2),
u.setDate(13),
f = u.getDay(),
f != 0 ? u.setDate(8 - f) : u.setDate(1),
i.setMonth(9),
i.setHours(1),
i.setDate(31),
f = i.getDay(),
i.setDate(31 - f),
r = new Date(),
r.setMonth(o),
r.setYear(h),
r.setDate(s),
r.setHours(e),
r >= u && r < i && (c = 1));
}
<html>
<head>
<script type="text/jscript" src="./clock.js"></script>
</head>
<body>
<div class="clock">
New York:
<span id="NewYork"> </span>
Zulu:
<span id="Zulu"> </span>
</div>
</body>
</html>
答案 0 :(得分:1)
从开始设置超时时起,您将用作回调的小写“时间”,它不是函数,但是由于“()”而返回值
toolbarMenu.getItem(0).setEnabled(false);
如果我很好理解应该是:
setTimeout(time(), 1e3);
也许不足以使其正常运行,但是可以肯定的是,使其无法正常工作。
答案 1 :(得分:0)
您的代码是完全不可读的,您绝对应该重新考虑变量的命名和结构。从中略读两件事:
worldClock
将返回r >= u && r < i && (c = 1))
Time
函数的引用传递给setTimeout
,而是将函数调用的结果传递给了。您的意思是setTimeout(Time, 1e3)
Time
,您正在尝试将setTimeout
与time
一起使用。 JS区分大小写,请将您的函数重命名为time
如果该功能正常运行,我会感到惊讶,根据我的经验,不可读的代码就是不可靠的代码。但是尝试一下,告诉我是否可行!祝你好运!
答案 2 :(得分:0)
让我们保持简单:
//<![CDATA[
/* js/external.js */
var doc, bod, I, ClockMaker; // for use on other loads
addEventListener('load', function(){
doc = document; bod = doc.body;
I = function(id){
return doc.getElementById(id);
}
ClockMaker = function(output, offset){
this.clocks = [];
var t = this;
function out(a){
var o = a[1] || 0, dt = new Date(3600000*o+Date.now());
a[0].innerHTML = dt.toUTCString();
}
setInterval(function(){
t.clocks.forEach(out);
}, 10);
this.clock = function(output, offset){
this.clocks.push([output, offset]);
return this;
}
}
var clocks = new ClockMaker;
clocks.clock(I('ny'), -5).clock(I('zu'));
}); // end load
//]]>
/* css/external.css */
html,body{
box-sizing:border-box; padding:0; margin:0;
}
h2{
margin:5px 7px;
}
h2,h2+div{
display:inline-block;
}
<!DOCTYPE html>
<html xmlns='http://www.w3.org/1999/xhtml' xml:lang='en' lang='en'>
<head>
<meta charset='UTF-8' /><meta name='viewport' content='width=device-width, height=device-height, initial-scale:1' />
<title>Simple Clock</title>
<link type='text/css' rel='stylesheet' href='css/external.css' />
<script type='text/javascript' src='js/external.js'></script>
</head>
<body>
<div><h2>New York:</h2><div id='ny'></div><div>
<div><h2>Zulu:</h2><div id='zu'></div></div>
</body>
</html>
您可以访问dt
来使用其他UTC方法。