这个错误的解决方案是什么?

时间:2016-04-30 03:51:35

标签: javascript arrays

这里我有一个Javascript文件 - 我知道代码现在非常混乱,但我只是编写一些基本功能而不是什么,然后我会清理代码。该页面有三个部分,这三个部分是图片。图片应该每30秒更换一次。导致此错误的原因是什么?

打印到控制台的错误:TypeError:undefined不是对象(评估'document.images [1] .src = images [alternate]')

以下是代码:

var alternate = 0;
var timerId;
var images = ["img/s1.jpg", "img/tourism.jpg", "img/s2.jpg", "img/shopping.jpg", "img/dining.jpg", "img/shopping2.jpg", "img/s3.jpg"]; // Image URLs in a image array

function startAnimation() {

    var now = new Date();
    var hours = now.getHours();
    var minutes = now.getMinutes();
    var seconds = now.getSeconds();
    var meridian = "AM";
    var time = hours + ":" + minutes + ":" + seconds + " " + meridian;

    if (hours < 12 && meridian == "PM" || hours == 0) {
        hours = hours + 12;
        meridian == "AM";
        time = hours + ":" + minutes + ":" + seconds + " " + meridian;
    } else if (hours > 12 && meridian == "AM") {
        meridian = "PM";
        hours = hours - 12;
        time = hours + ":" + minutes + ":" + seconds + " " + meridian;
    }
    //hours < 12 ? meridian : "AM";

    // comment on code here.
    if (minutes < 10) {
        minutes = "0" + minutes;
        time = hours + ":" + minutes + ":" + seconds + " " + meridian;
    }

    // comment on code here.
    if (seconds < 10) {
        seconds = "0" + seconds;
        time = hours + ":" + minutes + ":" + seconds + " " + meridian;
    }

    //alternate = (alternate == 0) ? 1 : 0; // Alternate images

    if (alternate == 0) {
        alternate = 3;
    } else {
        alternate = 0;
    }

    if (alternate == 1) {
        alternate = 2;
    } else {
        alternate = 1;
    }

    //document.images[0].src = images[alternate];  // Update image
    document.images[1].src = images[alternate];
    timerId = setTimeout("startAnimation()", 30000); // 30 second update

    if (images[alternate] == images[0]) {
        console.log(time + " " + images[0] + " has been loaded.");
    }

    if (images[alternate] == images[3]) {
        console.log(time + " " + images[3] + " has been loaded.");
    }

    if (images[alternate] != images[0] || images[alternate] != images[3]) {
        console.log("Please wait while the debugging process is in effect.");
    }

}

 startAnimation();

2 个答案:

答案 0 :(得分:1)

哟几乎没有错别字和错误:

meridian == "AM";

必须为meridian = "AM";

另外

timerId = setTimeout("startAnimation()", 30000);

必须:

setTimeout(startAnimation, 30000); 

答案 1 :(得分:0)

这是工作代码。发生错误:错误的setTimeout()实现和您使用的错误赋值运算符。

var alternate = 0;
var timerId;
var images = [
  "img/s1.jpg",
  "img/tourism.jpg",
  "img/s2.jpg",
  "img/shopping.jpg",
  "img/dining.jpg",
  "img/shopping2.jpg",
  "img/s3.jpg"
]; // Image URLs in a image array

function startAnimation() {

  var now = new Date();
  var hours = now.getHours();
  var minutes = now.getMinutes();
  var seconds = now.getSeconds();
  var meridian = "AM";
  var time = hours + ":" + minutes + ":" + seconds + " " + meridian;

  if (hours < 12 && meridian == "PM" || hours === 0) {
    hours = hours + 12;
    meridian = "AM";
    time = hours + ":" + minutes + ":" + seconds + " " + meridian;
  } else if (hours > 12 && meridian == "AM") {
    meridian = "PM";
    hours = hours - 12;
    time = hours + ":" + minutes + ":" + seconds + " " + meridian;
  }
  //hours < 12 ? meridian : "AM";

  // comment on code here.
  if (minutes < 10) {
    minutes = "0" + minutes;
    time = hours + ":" + minutes + ":" + seconds + " " + meridian;
  }

  // comment on code here.
  if (seconds < 10) {
    seconds = "0" + seconds;
    time = hours + ":" + minutes + ":" + seconds + " " + meridian;
  }

  //alternate = (alternate == 0) ? 1 : 0; // Alternate images

  if (alternate === 0) {
    alternate = 3;
  } else {
    alternate = 0;
  }

  if (alternate == 1) {
    alternate = 2;
  } else {
    alternate = 1;
  }

  //document.images[0].src = images[alternate];  // Update image
  document.images[0].src = images[alternate];
  timerId = setTimeout(startAnimation(), 30000); // 30 second update

  if (images[alternate] == images[0]) {
    console.log(time + " " + images[0] + " has been loaded.");
  }

  if (images[alternate] == images[3]) {
    console.log(time + " " + images[3] + " has been loaded.");
  }

  if (images[alternate] != images[0] || images[alternate] != images[3]) {
    console.log("Please wait while the debugging process is in effect.");
  }

}

startAnimation();
<img src="" alt="">