gpt4 book ai didi

javascript - HTML 表格只有一半时间加载 jQuery

转载 作者:可可西里 更新时间:2023-11-01 13:01:39 25 4
gpt4 key购买 nike

<分区>

我一直在尝试为我一直在做的 Smart Mirror 元素制作一个股票装载机。我遇到的问题是测试我的代码。出于某种原因,当我编译代码时,有一半时间表不会加载。我的编码经验水平是“业余爱好者”,所以有一个更改我犯了一个错误并且不知道。

但由于代码只有一半时间有效,我不知道问题出在哪里。我已经用不同版本的 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

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com