Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
303 views
in Technique[技术] by (71.8m points)

jQuery Ajax request every 30 seconds

I have this piece of code, but the values may change while someone is on my site. I would need to update the #finance div every 30 seconds or so. Can this be done?

$(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"
        });
});
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You can put your code in a separate function like this:

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" 
        });
    });
}

And then set up a timer calling the function every 30 seconds:

setInterval( LoadFinance, 30000 );

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...