簡體   English   中英

我如何從該網站獲取更新價格?

[英]How can I get the updated price from this site?

因此,我正在將一個基本的財務站點作為一個項目,但是在弄清原始價格並根據新價格進行處理之后,我不知道如何獲取新價格。

這就是我現在所擁有的

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" } ]

使用setInterval()輪詢服務器以獲取新價格。

這將每5000ms(5秒)檢查一次:

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 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input type="text" id="stock"> <div id="stockprice"></div> 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM