0

I'm trying to perform a function after a jQuery while loop has finished cycling through its settings. The loops are near the bottom

Here is my current script:

$(document).ready(function() {


$("#radial_container").radmenu({
    listClass: 'list', // the list class to look within for items
    itemClass: 'item', // the items - NOTE: the HTML inside the item is copied into the menu item
    radius: 150, // radius in pixels
    animSpeed:400, // animation speed in millis
    centerX: 160, // the center x axis offset
    centerY: 150, // the center y axis offset
    selectEvent: "click", // the select event (click)
    activeItemClass: "active",
    angleOffset: 185, // in degrees
    onSelect: function ($selected) {
        $(".radial_div_item").animate({
            //opacity: 0.5
        }, 0);
        $selected.animate({
            //opacity: 1
        }, 0);
        var inner = $selected.html();
        var i = 0;
        if ($selected.index() > 3) {
            while (i < $selected.index()) {
                $("#radial_container").radmenu("next");
                //Do something once loop finishes
            }
        } else {
            while ($selected.index() > i) {
                $("#radial_container").radmenu("prev");
                //Do something once loop finishes
            }
        }
    }

});
});

A friend has suggested I do something like this but it has not worked for me so far:

var looping = function(callback){ 
  *while loop* 
  callback();
}

I have already read through this post but was unable to find a solution in there linke is here.

I haven't included my markup because it seemed unnecessary but if it is needed I can post it up.

Thanks all.

2 Answers 2

1

First of all it looks like your loops are reorccouring and will never end.

while ($selected.index() > i) {
    $("#radial_container").radmenu("prev");
    //Do something once loop finishes
}

Unless this code $("#radial_container").radmenu("prev"); automatically increments the index() of $selected this loop will go on for ever, so you need to look into either manually incrementing your $selected.index() or i after each iteration of the loop.

But the answer to this question is quite simple, you don't have to run your function inside the loop, you can run it outside of the loop but within the if statement. So once the loop has finished iterating the function will run, like so.

if ($selected.index() > 3) {
    while (i < $selected.index()) {
        $("#radial_container").radmenu("next");
    }
    runFunctionHere();
} else {
    while ($selected.index() > i) {
        $("#radial_container").radmenu("prev");
    }
    runFunctionHere();
}

I hope that helps.

Sign up to request clarification or add additional context in comments.

Comments

0

But if you just don't wrap it like that:

while ($selected.index() > i) {
    $("#radial_container").radmenu("prev");
}
//Do something once loop finishes

It will work! Or i missing something?

3 Comments

Thanks for your reply. Thats what I thought but if I put it there it fires straight away and does not wait for the loop to finish. Any other thoughts? Thanks again.
Erm well the code you can see in the loop now is the only code in there. If you mean what does the $O('').radmenu()('prev') do. it is a function thats part of a plugin and it moves a sort of carousel round one position. I'm pretty sure I'm not suing any async functions, althoigh I could be wrong :S.
It doesn´t work, I have two small functions with animations within a while loop, and under I have a function to hide a div, I can barely see the first animation start before it is hidden, so it seems to work asynchronously I know in C# the equivalent code would work, so if anyone could explain the functionality, please :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.