1

I made this thumbnail rotator in javascript, and figured it could be written shorter as seen in the first example, however will this help performance? and are there any general guidelines you should follow in order to maximize javascript speed? (pardon my ugly code, I'm very new to the game)

function mouseOverfirst(videoId){
            var num = 8;
            var numb = 7;
            $('#'+videoId).attr('src',videoId+'/thumb'+numb+'.png');
            if(numb == 7){
            interval = setInterval(function(){
             $('#'+videoId).attr('src',videoId+'/thumb'+num+'.png');   
                if(num == 15){
                    num = 1;
                }    
                else
                {
                    num++;
                }
            },500);
        }
    }

or this?

function mouseOverfirst(videoId){
            var numb = 7;
            $('#'+videoId).attr('src',videoId+'/thumb'+numb+'.png');
            if(numb == 7){
                mouseOver('video1');
            }
}

function mouseOver(videoId){
            var num = 8;
            interval = setInterval(function(){
             $('#'+videoId).attr('src',videoId+'/thumb'+num+'.png');   
                if(num == 15){
                    num = 1;
                }    
                else
                {
                    num++;
                }
            },500);
        }
6
  • What exactly are you trying to accomplish? did you make separate slides of the image rotating? Sep 11, 2016 at 7:46
  • I doesn't seem as if your variables num or numb can change after declaration, so why do you need the if statement? Sep 11, 2016 at 8:08
  • You seem to care about performance, yet you use $("#"+videoId).attr('src', ...) instead of the indescribably faster document.getElementById(videoId).src = ... - you're looking for optimisations in the wrong places. Sep 11, 2016 at 8:09
  • If you want to make it run faster, decrease the interval from 500ms to, say, 200ms. SCNR
    – Bergi
    Sep 11, 2016 at 10:59
  • This code is absolutely not performance-sensitive. Don't try to optimise that. Rather, learn to write more beautiful code!
    – Bergi
    Sep 11, 2016 at 11:00

1 Answer 1

0

The complexity of both programs are the same.

It doesn't matter which one you use, but the 2nd one is more approachable as a function mouseOverFirst and mouseOver can be reused easily.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.