所以我正在制作一个基本的金融网站作为一个项目,但是在得到原价并根据新价格做事之后,我无法弄清楚如何获得新价格。
这就是我现在所拥有的
function getPrice() {
$.ajax({
url: 'http://finance.google.com/finance/info?client=ig&q=' + $("#stock").val(),
dataType: 'jsonp',
success: function(json) {
$("#stockprice").html(json[0]['l']);
}
});
}
$("#getstock").click(getPrice);
我只需要获得更新的价格,然后我会从那里开始。
例如,如果用户键入“AMD”,则JSON将如下所示
// [ { "id": "327" ,"t" : "AMD" ,"e" : "NASDAQ" ,"l" : "13.74" ,"l_fix" : "13.74" ,"l_cur" : "13.74" ,"s": "0" ,"ltt":"3:04PM EDT" ,"lt" : "Mar 29, 3:04PM EDT" ,"lt_dts" : "2017-03-29T15:04:22Z" ,"c" : "+0.05" ,"c_fix" : "0.05" ,"cp" : "0.37" ,"cp_fix" : "0.37" ,"ccol" : "chg" ,"pcls_fix" : "13.69" } ]
答案 0 :(得分:0)
使用setInterval()轮询服务器以获得新价格。
这将每隔5000毫秒(5秒)检查一次:
{
"checked":true,
"content": {
"text_field_1": {
"size":"small",
"text":"My userdefined Title",
"title":"Title"
},
"text_field_2": {
"size":"big",
"text":"I don't have inspiration",
"title":"Nachricht"
}
},
"details_sample":27,
"preview_user_title": "This is my User Title"
}
<强>段:强>
setInterval(getPrice, 5000);
function getPrice() {
if($('#stock').val()) {
$.ajax({
url: 'https://finance.google.com/finance/info?client=ig&q=' + $("#stock").val(),
dataType: 'jsonp',
success: function(json) {
$("#stockprice").html(json[0]['l']);
//do something with the new price
}
});
}
}
getPrice();
setInterval(getPrice, 5000);
$('#stock').change(getPrice); //get new price immediately when the input has changed