【问题标题】:iteration, JSON and callback in JSJS中的迭代、JSON和回调
【发布时间】:2015-05-05 19:24:00
【问题描述】:

(使用 Google Traductor 翻译) 嗨,我正在学习 javascript,我正在尝试模拟 yahoo Finance 页面上股票报价发生的变化。

我正在阅读来自 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);
		}
	});
} 
});

【问题讨论】:

    标签: javascript json callback jsonp yql


    【解决方案1】:
    setInterval(update, 0);
    

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

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

    【讨论】:

    • 考虑它是否真的需要尽可能快地运行。 setTimeoutsuccesserror 处理程序中可能会有所帮助。
    猜你喜欢
    • 2012-07-27
    • 2019-09-18
    • 2019-09-16
    • 1970-01-01
    • 2012-02-04
    • 1970-01-01
    • 2018-08-01
    • 1970-01-01
    • 2017-03-12
    相关资源
    最近更新 更多