views:

494

answers:

2

Ok here is the situation: I am using the basic example for jcarousel (Carousel with dynamic content loading via Ajax from a PHP script) as a basis:

    function mycarousel_itemLoadCallback(carousel, state)
{
    // Check if the requested items already exist
    if (carousel.has(carousel.first, carousel.last)) {
        return;
    }

    jQuery.get(
        'dynamic_ajax_php.php',
        {
            first: carousel.first,
            last: carousel.last
        },
        function(xml) {
            mycarousel_itemAddCallback(carousel, carousel.first, carousel.last, xml);
        },
        'xml'
    );
};

function mycarousel_itemAddCallback(carousel, first, last, xml)
{
    // Set the size of the carousel
    carousel.size(parseInt(jQuery('total', xml).text()));

    jQuery('image', xml).each(function(i) {
        carousel.add(first + i, mycarousel_getItemHTML(jQuery(this).text()));
    });
};

/**
 * Item html creation helper.
 */
function mycarousel_getItemHTML(url)
{
    return '<img src="' + url + '" width="75" height="75" alt="" />';
};

jQuery(document).ready(function() {
    jQuery('#mycarousel').jcarousel({
        // Uncomment the following option if you want items
        // which are outside the visible range to be removed
        // from the DOM.
        // Useful for carousels with MANY items.

        // itemVisibleOutCallback: {onAfterAnimation: function(carousel, item, i, state, evt) { carousel.remove(i); }},
        itemLoadCallback: mycarousel_itemLoadCallback
    });
});

What I would like to do is to execute a function right after the carousel has finished initializing and the images are in the dom and accessible. This however should only happen on pageload. Not everytime a previous or next button has been pressed. Any help in this matter is greatly appreciated.

Thank you

+1  A: 

You could use the initCallback.

$(function(){

  $("#mycarousel").jcarousel({
    initCallback: function(){
      alert("jCarousel is initialized!");
    }
  });

});

Using the Ajax Option

Since you indicated in the comments that you're using the ajax option, to load your images at a later time, I'd suggest continuing to use itemLoadCallback, and simply add a flag variable to the mix.

var loadedfirstime = true;
$("#mycarousel").jcarousel({
  itemLoadCallback: function(){
    if (loadedfirsttime) {
      myFunction();
      loadedfirsttime = false;
    }
  }
});

By setting the flag, all future callbacks will not raise the same behavior.

Jonathan Sampson
Jonathan,thanks but this is not working as I am using the ajax option for the carousel, and the initCallback alerts before the images are actually in place.
Sam
I see, Sam. Sorry for overlooking that. Note my updated answer.
Jonathan Sampson
A: 

Jonathan, Thank you very much for your excellent and fast response. For some reason I could not get it to work, meaning that a function is being called automatically after the Carousel has been built and the images are available in the dom. But I did a rather less elegant way and used a timer set to 1sec delay - I know it is not the most elegant solution and definitely not 100% reliable but it works for now.

Thanks again for the super quick responses.

Sam