HTML表只加载了jQuery一半的时间

时间:2016-07-08 16:52:59

标签: javascript jquery html css

我一直在尝试为我一直在做的Smart Mirror项目制作一个库存加载器。我遇到的问题是测试我的代码。出于某种原因,当我编译代码时,表的一半时间不会加载。我的编码经验水平是"业余爱好者"所以有一个改变,我犯了一个错误,不知道它。

但由于代码工作的时间有一半,我不知道问题是什么。我用不同版本的jQuery测试过它,我仍然遇到同样的问题。

我做错了什么? 这可能是JSON Google财经电话被拒绝的问题吗?



var gstock = [{
  "title": "Apple",
  "code": "NASDAQ:AAPL"
}, {
  "title": "Lockheed Martin",
  "code": "NYSE:LMT"
}, {
  "title": "Exxon Mobil",
  "code": "NYSE:XOM"
}, {
  "title": "Bristow",
  "code": "NYSE:BRS"
}, {
  "title": "Boeing",
  "code": "NYSE:BA"
}, {
  "title": "Realty Income",
  "code": "NYSE:O"
}, {
  "title": "Activision Blizzard",
  "code": "NYSE:ATVI"
}, {
  "title": "Level 3 Communication",
  "code": "NYSE:LVLT"
}, {
  "title": "Disney",
  "code": "NYSE:DIS"
}, {
  "title": "Tesla",
  "code": "NYSE:TSLA"
}, {
  "title": "Advanced Micro Devices",
  "code": "NYSE:AMD"
}, {
  "title": "Amazon",
  "code": "NYSE:AMZN"
}, {
  "title": "Raytheon",
  "code": "NYSE:RTN"
}, {
  "title": "Fedex",
  "code": "NYSE:FDX"
}, {
  "title": "Deutsche Bank",
  "code": "NYSE:DB"
}, {
  "title": "Microsoft",
  "code": "NYSE:MSFT"
}, ];

// array to hold previous results...
var results = new Array(gstock.length);

$(document).ready(function() {
  // construct the table...
  for (var i = 0; i < gstock.length; i++) {
    var row =
      "<tr id=\"row_" + i + "\" style=\"height:20px\"><td>" + gstock[i].title + "</td>" +
      "<td id=\"symbol_" + i + "\" style=\"text-align:left\"></td>" +
      "<td id=\"price_" + i + "\" style=\"text-align:right\"></td>" +
      "<td id=\"price_change_" + i + "\" style=\"text-align:right\"></td>" +
      "<td id=\"percent_change_" + i + "\" style=\"text-align:right\"></td></tr>";
    if (i == 0) {
      $('#stocks').append(row);
    } else {
      $('#stocks tr:last').after(row);
    }
  }

  display_table();

  setInterval(function() {
    display_table();
  }, 5000);
});

function load_stock(i) {
  // Need this function so that i can be referenced correctly inside the callback...
  $.getJSON("https://finance.google.com/finance/info?client=ig&q=" + gstock[i].code + "&callback=?", function(response) {
    var stockInfo1 = response[0];

    console.log("here: " + i);
    // update key fields
    $("#symbol_" + i).html(stockInfo1.t);
    $("#price_" + i).html(stockInfo1.l);
    $("#price_change_" + i).html(stockInfo1.c);
    $("#percent_change_" + i).html(stockInfo1.cp);

    $("#percent_change_" + i).append("%");
    $("#symbol_" + i).css({
      "padding-left": "10px"
    });
    $("#percent_change_" + i).css({
      "padding-right": "10px"
    });


    if (stockInfo1.c > 0) {
      $("#percent_change_" + i).css({
        "color": "#70DB70"
      });
      $("#price_change_" + i).css({
        "color": "#70DB70"
      });
      $("#percent_change_" + i).prepend("+");
    } else {
      $("#percent_change_" + i).css({
        "color": "#FF0000"
      });
      $("#price_change_" + i).css({
        "color": "#FF0000"
      });
    }


    if (results.length > 0) {
      // other calculations here...
      // use records[i] to get the previous record
    }

    flash_background("#row_" + i, "#000000", "#000000");
    flash_background("#price_" + i, "#3342FF", "#000000");
    flash_background("#price_change_" + i, "#3342FF", "#000000");
    flash_background("#percent_change_" + i, "#3342FF", "#000000");

    // store the last record (for next time);
    results[i] = stockInfo1;
  });
}

function flash_background(id, col1, col2) {
  $(id).css("background-color", col1);
  setTimeout(function() {
    $(id).css("background-color", col2);
  }, 300);
}

function display_table() {
  for (var i = 0; i < gstock.length; i++) {
    load_stock(i);
  }
}
&#13;
caption {
  width: 25em;
  height: 1em;
  color: white;
  background-color: black;
  padding-bottom: 10px;
  font-weight: bold;
  text-decoration: underline;
  font-family: "Times New Roman", Times, serif;
}
.container1 {
  height: 20em;
  width: 25em;
  color: white;
  font-family: "Times New Roman", Times, serif;
}
table {
  border-collapse: collapse;
}
td {}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div class="container1">
  <table id="stocks">
    <caption>Equities</caption>
  </table>
</div>
&#13;
&#13;
&#13;

查看JSFiddle

1 个答案:

答案 0 :(得分:3)

是的,你的猜测是正确的,因为一些上市公司在纳斯达克上市,因此你的控制台上有一些失败的Google财经电话,因此该表缺少几行。只需更新你的JSON,一切都会好的

&#13;
&#13;
var gstock = [{"title":"Apple", "code":"NASDAQ:AAPL"}, 
{"title":"Lockheed Martin", "code":"NYSE:LMT"},
{"title":"Exxon Mobil", "code":"NYSE:XOM"},
  {"title":"Bristow", "code":"NYSE:BRS"},
  {"title":"Boeing", "code":"NYSE:BA"},
  {"title":"Realty Income", "code":"NYSE:O"},
  {"title":"Activision Blizzard", "code":"NASDAQ:ATVI"},
  {"title":"Level 3 Communication", "code":"NYSE:LVLT"},
  {"title":"Disney", "code":"NYSE:DIS"},
  {"title":"Tesla", "code":"NASDAQ:TSLA"},
  {"title":"Advanced Micro Devices", "code":"NYSE:AMD"},
  {"title":"Amazon", "code":"NASDAQ:AMZN"},
  {"title":"Raytheon", "code":"NYSE:RTN"},
  {"title":"Fedex", "code":"NYSE:FDX"},
  {"title":"Deutsche Bank", "code":"NYSE:DB"},
  {"title":"Microsoft", "code":"NASDAQ:MSFT"} ];

// array to hold previous results...
var results = new Array(gstock.length);

$(document).ready(function() {
  // construct the table...
  for (var i = 0; i < gstock.length; i++) {
    var row =
      "<tr id=\"row_" + i + "\" style=\"height:20px\"><td>" + gstock[i].title + "</td>" +
      "<td id=\"symbol_" + i + "\" style=\"text-align:left\"></td>" +
      "<td id=\"price_" + i + "\" style=\"text-align:right\"></td>" +
      "<td id=\"price_change_" + i + "\" style=\"text-align:right\"></td>" +
      "<td id=\"percent_change_" + i + "\" style=\"text-align:right\"></td></tr>";
    if (i == 0) {
      $('#stocks').append(row);
    } else {
      $('#stocks tr:last').after(row);
    }
  }

  display_table();

  setInterval(function() {
    display_table();
  }, 5000);
});

function load_stock(i) {
  // Need this function so that i can be referenced correctly inside the callback...
  $.getJSON("https://finance.google.com/finance/info?client=ig&q=" + gstock[i].code + "&callback=?", function(response) {
    var stockInfo1 = response[0];

    // update key fields
    $("#symbol_" + i).html(stockInfo1.t);
    $("#price_" + i).html(stockInfo1.l);
    $("#price_change_" + i).html(stockInfo1.c);
    $("#percent_change_" + i).html(stockInfo1.cp);

    $("#percent_change_" + i).append("%");
    $("#symbol_" + i).css({
      "padding-left": "10px"
    });
    $("#percent_change_" + i).css({
      "padding-right": "10px"
    });


    if (stockInfo1.c > 0) {
      $("#percent_change_" + i).css({
        "color": "#70DB70"
      });
      $("#price_change_" + i).css({
        "color": "#70DB70"
      });
      $("#percent_change_" + i).prepend("+");
    } else {
      $("#percent_change_" + i).css({
        "color": "#FF0000"
      });
      $("#price_change_" + i).css({
        "color": "#FF0000"
      });
    }


    if (results.length > 0) {
      // other calculations here...
      // use records[i] to get the previous record
    }

    flash_background("#row_" + i, "#000000", "#000000");
    flash_background("#price_" + i, "#3342FF", "#000000");
    flash_background("#price_change_" + i, "#3342FF", "#000000");
    flash_background("#percent_change_" + i, "#3342FF", "#000000");

    // store the last record (for next time);
    results[i] = stockInfo1;
  });
}

function flash_background(id, col1, col2) {
  $(id).css("background-color", col1);
  setTimeout(function() {
    $(id).css("background-color", col2);
  }, 300);
}

function display_table() {
  for (var i = 0; i < gstock.length; i++) {
    load_stock(i);
  }
}
&#13;
caption {
  width: 25em;
  height: 1em;
  color: white;
  background-color: black;
  padding-bottom: 10px;
  font-weight: bold;
  text-decoration: underline;
  font-family: "Times New Roman", Times, serif;
}
.container1 {
  height: 20em;
  width: 25em;
  color: white;
  font-family: "Times New Roman", Times, serif;
}
table {
  border-collapse: collapse;
}
td {}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div class="container1">
  <table id="stocks">
    <caption>Equities</caption>
  </table>
</div>
&#13;
&#13;
&#13;

小提琴:http://fiddle.jshell.net/21n8j9ya/32/