jQuery的AJAX请求每30秒 [英] jQuery Ajax request every 30 seconds

查看:864
本文介绍了jQuery的AJAX请求每30秒的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这块code,但该值可能改变,而有人在我的网站。我需要每30秒左右更新#finance股利。可以这样做?

  $(函数(){
    $ .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=?",

        功能(JSON){
          $('#财经)文本(json.query.results.quote.Change);
            //修补的有效载荷送入页面元素ID =狗
        });
});
 

解决方案

您可以把您的code在这样一个单独的函数:

 函数LoadFinance()
{
    $(函数(){
        $ .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=?",
        功能(JSON){$('#财经)文本(json.query.results.quote.Change)。
        //修补的有效载荷送入页面元素ID =狗});
    });
}
 

然后设置一个定时器调用该函数每30秒:

 的setInterval(LoadFinance,30000);
 

祝你好运! ;)

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

解决方案

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

Good luck! ;)

这篇关于jQuery的AJAX请求每30秒的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆