Tell me more ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

I'd like some tips on optimising the following code.

I'm a bit concerned that I'm 'polluting the global namespace' by not wrapping it in a function. The commented out 'var rotator { init {' came from a javascript tutorial but I'm not sure it's the JQuery way? Should I wrap it in $(function()... instead?

$(document).ready(function() {

//Promos rotation
var $accordionList = $('.accordion').find('li');

// var rotator = {
//      init: function() {
        var numberOfItems = $accordionList.length;
        var currentItem = 0;

        // Set first item to active
        $accordionList.eq(currentItem).addClass('active').find('.content').slideToggle(800, function() {});


        var infiniateLoop = setInterval(function() {

            if(currentItem == numberOfItems - 1){
                currentItem = 0;
            }
            else {
                currentItem++;
            }

            // Remove active class, if is has it, and close content
            $accordionList.parent().find('li.active').removeClass('active')
                .find('.content').slideToggle(800, function() {
            });

            // Add active class and open content
            $accordionList.eq(currentItem).addClass('active').find('.content').slideToggle(800, function() {
            });

        }, 4000 );

        //$('.accordion li').on('click', function () {
        $accordionList.on('click', function () {
            // Stop rotation
            clearInterval(infiniateLoop);

            var $accordionHead = $(this);

            // Remove active class, if is has it, and close content
            $accordionHead.parent().find('li.active').removeClass('active')
                .find('.content').slideToggle(800, function() {
            });

            // Add active class and open content
            $accordionHead.addClass('active').find('.content').slideToggle(800, function() {
            });
        });
//      }
// };

// rotator.init();

});

Any help and advice much appriciated

share|improve this question
1  
"I'm not sure it's the JQuery way?" jQuery way? $(function(){}) === $(document).ready(function(){}) – Raminson Dec 5 '12 at 10:54
you are wrapping it in a function... $(document.ready(function... – ahren Dec 5 '12 at 10:56
Your code as it is does not polute any global namespace, as it's all contained within a function being called by $(document).ready. The addition or removal of the rotator object does not change this fact. Either way it's wholey contained within a function – Jamiec Dec 5 '12 at 10:56
2  
Since you wrapped your code in $(document).ready() and you are using the var keyword you are surely not polluting the global namespace – devnull69 Dec 5 '12 at 10:56
yes but shouldn't I wrap it in another function so it doesn't conflict with other fucntion (yet to be written) that are also in the document.ready ? – Chris Dec 5 '12 at 10:57
show 1 more comment

migrated from stackoverflow.com Dec 5 '12 at 13:42

Know someone who can answer? Share a link to this question via email, Google+, Twitter, or Facebook.

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.