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);
}
num
ornumb
can change after declaration, so why do you need the if statement?$("#"+videoId).attr('src', ...)
instead of the indescribably fasterdocument.getElementById(videoId).src = ...
- you're looking for optimisations in the wrong places.