Search code examples
jquerycycle

Destroy cycle if span is the only element


I have the following code:

$.ajax({
    type: "GET",
    url: "AJAX.ashx?job=fetch_calendar",
    dataType: "html",
    success: function (html) {
        $("#calendar_system").cycle("destroy").html(html);
        if ($("#calendar_system:not(:has(span.no_calendar_items))")) {
            $("#calendar_system").cycle({ fx: "scrollUp", timeout: 6000, cleartype: 1, speed: 800 });
        }                   
    }
});

This updates a calendar list and cycles the items. But it also cycles even if has the span.no_calendar_items - why is that? I destroy the cycle before doing anything to make sure its gone. But it does not seem to be working.

The AJAX function is running every 10 minute, (setInterval()).

Why? :-)


Solution

  • ($("#calendar_system:not(:has(span.no_calendar_items))")) is a jQuery object, so it will always evaluate as true. You need to test if its length is greater than zero:

    if ($("#calendar_system:not(:has(span.no_calendar_items))").length) {