Random Div Shown on page refresh
2 posters
Page 1 of 1
Random Div Shown on page refresh
I have this jQuery
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?
- 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?
Re: Random Div Shown on page refresh
thank you I knew I was missing the function, I thought I needed the
tried placing it in there and but dont think I closed it maybe, not sure...
I'll let you know if it worked
- 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
Re: Random Div Shown on page refresh
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);
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- Hyperactive
- Posts : 2265
Reputation : 264
Language : English
Location : UK
Re: Random Div Shown on page refresh
The code is working just a dandy thank you, see I knew I was missing something the whole time 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
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
Re: Random Div Shown on page refresh
No your right. I'm going blind.
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.
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...
Just some food for thought
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);
});
Just some food for thought
LGforum- Hyperactive
- Posts : 2265
Reputation : 264
Language : English
Location : UK
Re: Random Div Shown on page refresh
yeah u can lock er up. was just asking if possible to add variable to show one div more than the others
Similar topics
» time shown at page
» Refresh iframe and not the page
» A Refresh Page Button
» Quick reply without page refresh!!!
» Click like button have to refresh forum page
» Refresh iframe and not the page
» A Refresh Page Button
» Quick reply without page refresh!!!
» Click like button have to refresh forum page
Page 1 of 1
Permissions in this forum:
You cannot reply to topics in this forum