HTML 表只加载 jQuery 一半的时间

HTML table only loads jQuery half the time

本文关键字:一半 时间 jQuery 加载 HTML      更新时间:2023-09-26

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

但是由于代码工作了一半的时间,我不知道问题可能是什么。我已经用不同版本的jQuery对其进行了测试,但仍然遇到相同的问题。

我做错了什么?
这可能是JSON Google Finance呼叫被拒绝的问题吗?

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);
  }
}
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 {}
<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>

在 JSFiddle 上查看

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

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);
  }
}
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 {}
<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>

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