JSLint错误:未声明的“图像”,未声明的“文档”超出范围

时间:2016-02-05 14:04:13

标签: javascript jslint

凭借非常基本的JavaScript知识,我构建了一个简单的幻灯片(下面的代码)。它可以工作,但JSLint显示以下错误:

  • 未声明的'图片' slideimages [0] =新图片();
  • 未声明的'setInterval' setInterval(myCounter,50);
  • 'myCounter'超出范围 setInterval(myCounter,50);
  • 未声明的'文档' document.getElementById('slide')。src = slideimages [step] .src;

我如何克服这些问题,例如声明那些元素?

var slideimages = [];

slideimages[0] = new Image();
slideimages[0].src = "http://placehold.it/350x150?text=Welcome";
slideimages[1] = new Image();
slideimages[1].src = "imgs/slide_1.png";
slideimages[2] = new Image();
slideimages[2].src = "imgs/slide_2.png";


var step = 0;

var c = 0;
var a = 0;
setInterval(myCounter, 50);
function myCounter() {

    'use strict';

document.getElementById('slide').src = slideimages[step].src;

    if ((c >= 0) && (c < 40)) {
        c += 1;
        a = c;
        step = 1;
document.getElementById("demo").innerHTML = a / 100;
document.getElementById("slide").style.opacity = a / 40;

    } else if ((c >= 40) && (c < 80)) {

        c += 1;
        a = c;
document.getElementById("demo").innerHTML = a / 100;
document.getElementById("slide").style.opacity = 1;

    } else if ((c >= 80) && (c < 100)) {

        c += 1;
        a = c;
document.getElementById("demo").innerHTML = 1 - ((a - 80) / 20);
document.getElementById("slide").style.opacity = 1 - ((a - 80) / 20);

    } else if ((c >= 100) && (c < 140)) {

        c += 1;
        a = c;
        step = 2;
document.getElementById("demo").innerHTML = a / 100;
document.getElementById("slide").style.opacity = (a - 100) / 40;

    } else if ((c >= 140) && (c < 180)) {

        c += 1;
        a = c;
document.getElementById("demo").innerHTML = a / 100;
document.getElementById("slide").style.opacity = 1;

    } else if ((c >= 180) && (c < 200)) {

        c += 1;
        a = c;
document.getElementById("demo").innerHTML = 1 - ((a - 180) / 20);
document.getElementById("slide").style.opacity = 1 - ((a - 180) / 20);

    } else {
        c = 0;
    }
}

1 个答案:

答案 0 :(得分:1)

默认情况下,LSLint不允许与浏览器相关的内容。因此,

Undeclared 'Image' slideimages[0] = new Image();
Undeclared 'setInterval' setInterval(myCounter, 50);
Undeclared 'document' document.getElementById('slide').src =slideimages[step].src;

查看jslint选项,以及assume browser中的特殊选项。