JS Onload在一个实例中工作但不是另一个实例

时间:2015-04-21 20:24:41

标签: javascript

我有这个代码,按ID显示常规时钟和八进制时钟。如果我在一个页面上只有一个时钟(只使用每个ID一次),它就可以工作,但如果我每个时钟有多个时钟(多次使用每个ID),那么后一个时钟就不会工作。什么可能阻止后一个时钟工作?

JS:

function checkTime(i) {
    'use strict';
    if (i < 10) {
        i = "0" + i;
    }
    return i;
}
function a() {
    'use strict';
    var oct = ["0", "1", "2", "3", "4", "5", "6", "7"],
        octtime,
        oct1,
        oct2,
        oct3,
        oct4,
        oct5,
        oct6,
        octvalue,
        point = ".",
        now = new Date(),
        hours = now.getHours(),
        minutes = now.getMinutes(),
        seconds = now.getSeconds(),
        h = checkTime(hours),
        m = checkTime(minutes),
        s = checkTime(seconds),
        totsecs = [hours * 3600 + minutes * 60 + seconds + (now.getTime() % 1000) / 1000];
    octtime = Math.floor(totsecs / (86400 / 262144));
    oct1 = Math.floor(octtime / 32768);
    octtime -= 32768 * oct1;
    oct2 = Math.floor(octtime / 4096);
    octtime -= 4096 * oct2;
    oct3 = Math.floor(octtime / 512);
    octtime -= 512 * oct3;
    oct4 = Math.floor(octtime / 64);
    octtime -= 64 * oct4;
    oct5 = Math.floor(octtime / 8);
    octtime -= 8 * oct5;
    oct6 = octtime;
    octvalue = point + oct[oct1] + oct[oct2] + oct[oct3] + oct[oct4] + oct[oct5] + oct[oct6];
    document.getElementById('j').innerHTML = h + ":" + m + ":" + s;
    document.getElementById('k').innerHTML = octvalue;
    window.setTimeout(a);
}
window.onload = a;

测试页面:http://www.gloryhood.com/articles/zztest.html

0 个答案:

没有答案