gpt4 book ai didi

jQuery Ajax 请求每 30 秒一次

转载 作者:行者123 更新时间:2023-12-03 22:05:55 25 4
gpt4 key购买 nike

我有这段代码,但当有人访问我的网站时,这些值可能会发生变化。我需要每 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 );

关于jQuery Ajax 请求每 30 秒一次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4450579/

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