![]() | |
![]() |
| | Thread Tools | Display Modes |
#1
| |||
| |||
|
|
Greetings. I download this java script and it does work. Actually, it is great for my purpose. Just a little complain: It will be great if it will open the ads randomly. Right now, it will open then in the order you see bellow. I don't know much about java. Could anyone modify this easely enough to do the random thing? Thanks. email: Norberto_Rivera (AT) yahoo (DOT) com ================================================== ===================== script language="JavaScript1.1" !-- /* JavaScript Image slideshow: By Website Abstraction (www.wsabstract.com) and Java-scripts.net (www.java-scripts.net) */ var slideimages=new Array() var slidelinks=new Array() function slideshowimages(){ for (i=0;i<slideshowimages.arguments.length;i++){ slideimages[i]=new Image() slideimages[i].src=slideshowimages.arguments[i] } } function slideshowlinks(){ for (i=0;i<slideshowlinks.arguments.length;i++) slidelinks[i]=slideshowlinks.arguments[i] } function gotoshow(){ if (!window.winslide||winslide.closed) winslide=window.open(slidelinks[whichlink]) else winslide.location=slidelinks[whichlink] winslide.focus() } //-- /script a href="javascript:gotoshow()"><img src="img1.gif" name="slide" border=0></a script !-- //configure the paths of the images, plus corresponding target links slideshowimages("anuncios/ANUNCIATEAQUI.gif", "anuncios/jazzlatino.gif", "anuncios/SIGNSRUSPR.gif") slideshowlinks("http://www.signsruspr.com", "http://http://www.geocities.com/norberto95aolcom/JazzLatino.html", "http://www.signsruspr.com") //configure the speed of the slideshow, in miliseconds var slideshowspeed=15000 var whichlink=0 var whichimage=0 function slideit(){ if (!document.images) return document.images.slide.src=slideimages[whichimage].src whichlink=whichimage if (whichimage<slideimages.length-1) whichimage++ else whichimage=0 setTimeout("slideit()",slideshowspeed) } slideit() //-- /script |
#2
| |||
| |||
|
|
This belongs in: comp.lang.javascript Followup-to: comp.lang.javascript Norberto Rivera wrote: Greetings. I download this java script and it does work. Actually, it is great for my purpose. Just a little complain: It will be great if it will open the ads randomly. Right now, it will open then in the order you see bellow. I don't know much about java. Could anyone modify this easely enough to do the random thing? Thanks. email: Norberto_Rivera (AT) yahoo (DOT) com ================================================== ===================== script language="JavaScript1.1" !-- /* JavaScript Image slideshow: By Website Abstraction (www.wsabstract.com) and Java-scripts.net (www.java-scripts.net) */ var slideimages=new Array() var slidelinks=new Array() function slideshowimages(){ for (i=0;i<slideshowimages.arguments.length;i++){ slideimages[i]=new Image() slideimages[i].src=slideshowimages.arguments[i] } } function slideshowlinks(){ for (i=0;i<slideshowlinks.arguments.length;i++) slidelinks[i]=slideshowlinks.arguments[i] } function gotoshow(){ if (!window.winslide||winslide.closed) winslide=window.open(slidelinks[whichlink]) else winslide.location=slidelinks[whichlink] winslide.focus() } //-- /script a href="javascript:gotoshow()"><img src="img1.gif" name="slide" border=0></a script !-- //configure the paths of the images, plus corresponding target links slideshowimages("anuncios/ANUNCIATEAQUI.gif", "anuncios/jazzlatino.gif", "anuncios/SIGNSRUSPR.gif") slideshowlinks("http://www.signsruspr.com", "http://http://www.geocities.com/norberto95aolcom/JazzLatino.html", "http://www.signsruspr.com") //configure the speed of the slideshow, in miliseconds var slideshowspeed=15000 var whichlink=0 var whichimage=0 function slideit(){ if (!document.images) return document.images.slide.src=slideimages[whichimage].src whichlink=whichimage if (whichimage<slideimages.length-1) whichimage++ else whichimage=0 setTimeout("slideit()",slideshowspeed) } slideit() //-- /script |
#3
| |||
| |||
|
|
Read the FAQ: http://www.jibbering.com/faq/ You need to look at the Math object. It has a random() function that returns a random number between 0 and 1. |
|
So, for instance, you might try doing something like this: whichimage = Math.round(Math.random() * 1009) % slideimages.length; Here, we create a random number, multiply it against a significantly large prime number, and round it to an integer. We then mod it (basically a remainder) from the lengths of your slide array. This will always present a pseudo-random number between 0 and slideimages.length - 1. |
#4
| |||
| |||
|
|
So, for instance, you might try doing something like this: whichimage = Math.round(Math.random() * 1009) % slideimages.length; Here, we create a random number, multiply it against a significantly large prime number, and round it to an integer. We then mod it (basically a remainder) from the lengths of your slide array. This will always present a pseudo-random number between 0 and slideimages.length - 1. A bad suggestion. It generates a distribution which is not uniform, except for the case of one image only. Consider the case of 670 images; the first half will have approximately twice the frequency of the second half. You, too, should read the FAQ, which gives a mathematically sound solution providing a uniform distribution (and only needs a %1 in the right place to protect against the aforementioned bug). Those who try to reinvent the wheel have a duty to check its performance against those of existing standard wheels before recommending their invention to the public. |
#5
| |||
| |||
|
|
I made an assumption of a low number of images when I chose the number 1009... For an image set as large as 670 images, a much higher number would be necessary for the distribution to be even. |
|
This obviously is not a solution that supports maintainability. A developer does not want to change the multiplier every time the image set grows. |

#6
| |||
| |||
|
|
Using the number of images is a lot easier to maintain, especially if you have them in an array ![]() /L |

![]() |
| Thread Tools | |
| Display Modes | |
| |