Adam Ramadhan
Adam Ramadhan

Reputation: 22820

reloading a function or a var in jquery ( newbie )

jQuery(document).ready(function(){

    // ok im geting the value here maybe i should make it in one var or in a function
    var feedid = $('.feeds .feed:first').attr('id');
    var feedid = feedid.match(/^feed-(\d+)$/);

    if (feedid){
        setInterval(function() {

            $.ajax({
                type: 'GET',
                url: '/area51/ajax',
                context: $("#ajax-information"),
                data: { f: feedid[1] },
                success: function(data) {

                    $(this).html(data);

                    var feedstatus = $('.ajax-status').attr('id');
                    if (feedstatus == 'none') {
                        $(this).hide();     
                    };

                    if (feedstatus == 'true') {
                        $(this).fadeIn().html(data);
                        $('#ajax-information a').click(function(e) {
                            $('.feeds').fadeIn().load('/ .feeds');
                            $('#ajax-information').fadeOut();
                            e.preventDefault();
                        });
                                            // this is what im trying to do
                        feedid.load();
                    };
                }
            });
        }, 7000);
    };
});

im trying to reload so my feedid is refreshed, is there a way doing this?

edit*

  function getNewFeed(){
      var feedid = $('.feeds .feed:first').attr('id');
      var feedid = feedid.match(/^feed-(\d+)$/);

      if (feedid){
        $.ajax({
            type: 'GET',
            url: '/area51/ajax',
            context: $("#ajax-information"),
            data: { f: feedid[1] },
            success: function(data) {

                $(this).html(data);

                var feedstatus = $('.ajax-status').attr('id');
                if (feedstatus == 'none') {
                    $(this).hide();     
                };

                if (feedstatus == 'true') {
                    $(this).fadeIn().html(data);
                    $('#ajax-information a').click(function(e) {
                        //I commented the following line because (as i understand)    
                        //getNewFeed() makes the ajax call
                        $('.feeds').fadeIn().load('/ .feeds');
                        $('#ajax-information').fadeOut();
                        e.preventDefault();
                    });
                };
            }
        });
    }
}

jQuery(document).ready(function(){
     setInterval(function() {
        getNewFeed();            
     }, 7000);       
});

this one works great! anyway if there is any way better doing this please post it.

thanks for looking in!

Adam Ramadhan

Upvotes: 2

Views: 124

Answers (1)

Edgar Villegas Alvarado
Edgar Villegas Alvarado

Reputation: 18354

If you want to reload every 7 seconds, or when the link is pressed (it isn't clear for what your classes and ids are), I think this should be the way:

  function getNewFeed(){
      var feedid = $('.feeds .feed:first').attr('id');
      var feedid = feedid.match(/^feed-(\d+)$/);

      if (feedid){
        $.ajax({
            type: 'GET',
            url: '/area51/ajax',
            context: $("#ajax-information"),
            data: { f: feedid[1] },
            success: function(data) {

                $(this).html(data);

                var feedstatus = $('.ajax-status').attr('id');
                if (feedstatus == 'none') {
                    $(this).hide();     
                };

                if (feedstatus == 'true') {
                    $(this).fadeIn().html(data);
                    $('#ajax-information a').click(function(e) {
                        //I commented the following line because (as i understand)    
                        //getNewFeed() makes the ajax call
                        //$('.feeds').fadeIn().load('/ .feeds');
                        getNewFeed();  //We force to get new feed with click
                        $('#ajax-information').fadeOut();
                        e.preventDefault();
                    });
                };
            }
        });
    }
}

jQuery(document).ready(function(){
     setInterval(function() {
        getNewFeed();            
     }, 7000);       
});

Hope this helps

Upvotes: 1

Related Questions