gpt4 book ai didi

javascript - JS 中的迭代、JSON 和回调

转载 作者:行者123 更新时间:2023-11-28 07:33:28 26 4
gpt4 key购买 nike

(使用 Google Traductor 翻译)你好,我正在学习 javascript,我正在尝试模拟雅虎财经页面上股票报价中发生的变化。

我在 stackoverflow 页面上阅读了很多回复,然后我就这么做了。

http://js.do/code/53880如果您使用谷歌浏览器,大约一分钟后进入控制台就会出现错误。Control -Shift -J(控制台选项卡/错误)

错误:net::ERR_INSUFFICIENT_RESOURCESjquery-1.9.1.js:8336 sendjquery-1.9.1.js:7978 jQuery.extend.ajaxVM8678:12 更新

页面也有延迟

$(function(){
$(function() {
setInterval(update, 0);
});


function update() {
var query = "select * from yahoo.finance.quotes where symbol = ";
var symbolo = "'AAPL'";
var yql = "http://query.yahooapis.com/v1/public/yql?q=" + escape(query+symbolo) + "&format=json&diagnostics=true&env=store://datatables.org/alltableswithkeys&callback=?";
var xhr2 = $.ajax({
url: yql,
jsonp: "myCallback",
dataType: 'jsonp',
success: function(data) {
var keys = data.query.results.quote;
$("#a").html(keys.LastTradePriceOnly);
},
error: function (xhr, ajaxOptions, thrownError) {
alert("oth1"+xhr.statusText);
alert("oth2"+xhr.responseText);
alert("oth3"+xhr.status);
alert("oth4"+thrownError);
}
});
}
});

最佳答案

setInterval(update, 0);

您会尽可能快地产生 Ajax 请求。然后你就会耗尽资源(可能在第一个请求被处理之前)。

运行update函数一次,然后从成功内部再次调用它(最好使用基于setTimeout的延迟) error 函数。

关于javascript - JS 中的迭代、JSON 和回调,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28860673/

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