我有这段代码,但是当有人在我的网站上时,值可能会更改。我需要每30秒左右更新#finance div。能做到吗?
$(function() { $.getJSON( "http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20yahoo.finance.quotes%20where%20symbol%20in%20(%22%5EFTSE%22)%0A%09%09&format=json&env=http%3A%2F%2Fdatatables.org%2Falltables.env&callback=?", function(json){ $('#finance').text(json.query.results.quote.Change); // Patching payload into page element ID = "dog" }); });
您可以将代码放在这样的单独函数中:
function LoadFinance() { $(function() { $.getJSON( "http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20yahoo.finance.quotes%20where%20symbol%20in%20(%22%5EFTSE%22)%0A%09%09&format=json&env=http%3A%2F%2Fdatatables.org%2Falltables.env&callback=?", function(json){ $('#finance').text(json.query.results.quote.Change); // Patching payload into page element ID = "dog" }); }); }
然后设置一个计时器,每30秒调用一次该函数:
setInterval( LoadFinance, 30000 );
祝好运!;)