简体   繁体   English

在定义之前使用了“ setTimeout”

[英]'setTimeout' was used before it was defined

This is the only error I'm getting for my code now. 这是我现在获取代码的唯一错误。

'setTimeout' was used before it was defined. 在定义之前使用了“ setTimeout”。

It occurs due to this line: 由于此行而发生:

setTimeout("a()");

What should I do about it in order to pass validation? 为了通过验证,我该怎么办? This is my full code: 这是我的完整代码:

/*global document, window */
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('a').innerHTML = h + ":" + m + ":" + s;
    document.getElementById('b').innerHTML = octvalue;
    setTimeout("a()");
}
window.onload = a;

If you're developing for a browser environment - declare it: set the environment instead: 如果您正在开发浏览器环境,请声明它: 设置环境

/*jslint browser: true, devel: true */

This will enable everything in browsers - so you can drop the window/document globals too. 这将启用浏览器中的所有功能-因此您也可以删除窗口/文档全局变量。

While you're at it, pass it the function rather than a string 在使用它时,将其传递给函数而不是字符串

setTimeout(a); // and not "a()"

I think that you could try in this way, you have already this correct line 我认为您可以以这种方式尝试,您已经有了正确的路线

/*global document, window */

just call setTimeout with the prefix of window 只需调用带有window前缀的setTimeout

window.setTimeout(a);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM