The forum of the forums
Would you like to react to this message? Create an account in a few clicks or log in to continue.

Random Div Shown on page refresh

2 posters

Go down

Solved Random Div Shown on page refresh

Post by nextlevelgaming October 6th 2012, 3:28 am

I have this jQuery

Code:

var whichToShow = Math.floor(Math.random() * $('.quote').length);
$('.quote').hide().eq(whichToShow).fadeIn(1000);

It suppose to change my shown div each page refresh (basically I am making a random advertiser)

though it is not working, I don't know if there is more needed to the code can LG or rideem help?
nextlevelgaming
nextlevelgaming
Forumember

Male Posts : 989
Reputation : 38
Language : English|CSS|HTML5|javascript|
Location : New York

http://www.easybbtutorials.forumotion.com

Back to top Go down

Solved Re: Random Div Shown on page refresh

Post by nextlevelgaming October 7th 2012, 3:00 am

thank you I knew I was missing the function, I thought I needed the
Code:
$(document).ready(function() {

tried placing it in there and but dont think I closed it maybe, not sure...

I'll let you know if it worked
nextlevelgaming
nextlevelgaming
Forumember

Male Posts : 989
Reputation : 38
Language : English|CSS|HTML5|javascript|
Location : New York

http://www.easybbtutorials.forumotion.com

Back to top Go down

Solved Re: Random Div Shown on page refresh

Post by LGforum October 7th 2012, 3:35 am

Don't forget, Math.random() returns a random floating point number between 0 and 1. You have to fix it, by multiplication which your doing.

Then you'll need to round it up or down using Math.ceil or Math.floor to make it a whole integer.

Random whole number between 0 and 10: Math.floor(Math.random * 10);
LGforum
LGforum
Hyperactive

Male Posts : 2265
Reputation : 264
Language : English
Location : UK

Back to top Go down

Solved Re: Random Div Shown on page refresh

Post by nextlevelgaming October 7th 2012, 4:10 am

The code is working just a dandy thank you, see I knew I was missing something the whole time Wink thanks a bunch rideem. +1 for ya.

LG what are you speaking of? i have a math.floor(math.random * $


Which seems to be working fine. If you could elaborate a bit I could understand, I mean is my code missing something more? or is it ok, its just going to randomly show an ad.

Also anyway I could make it so one div (ad) will show more than the others??? just curious
nextlevelgaming
nextlevelgaming
Forumember

Male Posts : 989
Reputation : 38
Language : English|CSS|HTML5|javascript|
Location : New York

http://www.easybbtutorials.forumotion.com

Back to top Go down

Solved Re: Random Div Shown on page refresh

Post by LGforum October 7th 2012, 5:57 pm

No your right. I'm going blind. Razz

For some reason I didn't see the rounding in your code...
But my question would be, why use the slow ':eq()' selector. The '.eq()' method will be quicker, like what you had at first... but even quicker than that would just be accessing the element by index.
Code:
$(function() {
  var whichToShow = Math.floor(Math.random() * $('.quote').length);
  var div = $('.quote')[whichToShow];
  if(div) $(div).fadeIn(1000);
});

Notice accessing it through index in the third line. No slow selectors and no extra function call. I added the IF, because your using the length property to limit the random number, which potentially could return a value out of range since the max is inclusive.

For example, you have 2 DIVs; div[0] and div[1]. A random number between 0 & 2 (inclusive) could return 2. div[2] does not exist and you'll hit an error.

Alternatively, you could change it to: Math.floor(Math.random() * $('.quote').length - 1);
Taking 1 off of the length will mean you don't have to perform that IF. As it will mean a random number between 0 & 1. Both div[0] and div[1] exist.

And even further improvement would be not using the same selector twice...
Code:
$(function() {
  var quotes = $('.quote');
  var index = Math.floor(Math.random() * quotes.length - 1);
  $(quotes[index]).fadeIn(1000);
});
Notice how your no longer parsing and performing the selector '.quote' twice anymore. Just the once and storing it in a variable.
Just some food for thought Wink
LGforum
LGforum
Hyperactive

Male Posts : 2265
Reputation : 264
Language : English
Location : UK

Back to top Go down

Solved Re: Random Div Shown on page refresh

Post by nextlevelgaming October 7th 2012, 10:02 pm

yeah u can lock er up. was just asking if possible to add variable to show one div more than the others
nextlevelgaming
nextlevelgaming
Forumember

Male Posts : 989
Reputation : 38
Language : English|CSS|HTML5|javascript|
Location : New York

http://www.easybbtutorials.forumotion.com

Back to top Go down

Back to top

- Similar topics

 
Permissions in this forum:
You cannot reply to topics in this forum