HTML 表只加载 jQuery 一半的时间
HTML table only loads jQuery half the time
我一直在尝试为我一直在做的智能镜子项目制作一个库存加载器。我遇到的问题是测试我的代码。出于某种原因,当我编译代码时,有一半的时间表不会加载。我的编码经验水平是"业余爱好者",所以有一个变化,我犯了一个错误,不知道。
但是由于代码工作了一半的时间,我不知道问题可能是什么。我已经用不同版本的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/
相关文章:
- Java脚本时间添加
- Json数据包含日期和时间格式
- 如何根据时间运行不同的脚本
- 用每小时的差值填充数组/列表-从下拉列表中给定两个时间值
- 在wordpress一定时间后更改自定义字段
- 更改angularjs中的日期-时间格式
- 将当前时间添加到我的页面上的特定部分
- 门户网站:当地时间有多有用
- 显示时间的脚本
- 引导下拉列表只工作一半的时间
- Phantom JS:第一个孩子返回正确结果的时间有一半
- Javascript加载功能只能在一半的时间内工作(Firefox)
- 如何在一半时间使用javascript完成在线考试时显示警告框
- Angular.js只加载一半的时间
- 零位板的工作时间有一半
- 为什么ngDraggable在angular ng-repeat中只工作一半的时间?
- 获得空白图像与画布绘制大约一半的时间
- shadowbox.js不起作用,只在一半时间内正常打开
- 为什么游戏时间缩短了一半而不是2秒
- HTML 表只加载 jQuery 一半的时间