weixin_33730836 2014-08-13 01:42 采纳率: 0%
浏览 12

Ajax通过计数器自动加载

I got this code from some template, it gets executed by clicking on the tabs to fetch posts into the page. All I want is to have an edited copy of this code to fetch posts by timer aside from clicking on the tabs. I have tried the setInterval but it didn't work, I appreciate any help I am so new to Ajax and JQuery.

jQuery(document).ready(function($) {

    setInterval(function(){

        e.preventDefault();
        var bt = $(this);
        var bts = bt.parent().parent();
        var where = $(this).parent().parent().parent().next();
        var nbs = bt.parent().parent().data('nbs');
        var nop = bt.parent().parent().data('number_of_posts');

        cat = bt.data('cat_id');
        if (cat === '') {
            cat = bt.data('parent_cat');
        }
        where.parent().find('.show-more').find('.nomoreposts').remove();

        jQuery.ajax({
            type: "post",
            url: nbtabs.url,
                        dataType: 'html',
                        data: "action=nbtabs&nonce="+nbtabs.nonce+"&cat="+cat+"&nbs="+nbs+"&number_of_posts="+nop,
            cach: false,
            beforeSend : function () {
                where.parent().append('<i class="nb-load"></i>');
            },
            success: function(data){
                where.hide().html(data).fadeIn('slow');
                bts.find('li').removeClass('active');
                bt.parent().addClass('active');
                where.parent().find('.nb-load').remove();
            }
        });
    }, 5000)

})
  • 写回答

2条回答 默认 最新

  • weixin_33725272 2014-08-13 02:09
    关注

    You have to get started to some degree before we can really help you code-wise. We can't just write the code for you because we do not know what elements you want updated and how.

    All I can advise you is the Jquery Ajax method is how this code retrieves url responses:

    jQuery.ajax({
            type: "post",
            url: "<name of your url or maybe servlet>"
            success: function(data){
               // data is the response from your url
               // in the code sample, data was html that was inserted to an element
            }
        });
    

    You can put this ajax call in a function and use setInterval. You can place the setInterval call on your Jquery.ready() function.

    评论

报告相同问题?