slide-show (random images)?

M

Michael Burtenshaw

I would like to make a slide show using random images. The problem is my
host is 250.com, and they don't support cgi-programs. Is there another
way to accomplish random images?
 
R

Richard

Michael said:
I would like to make a slide show using random images. The problem is my
host is 250.com, and they don't support cgi-programs. Is there another
way to accomplish random images?


www.dynamicdrive.com

I thought 250.com went out of the hosting business.
Since I now get a website for "micropatent".
 
E

Evertjan.

Michael Burtenshaw wrote on 19 dec 2004 in comp.lang.javascript:
I would like to make a slide show using random images. The problem is my
host is 250.com, and they don't support cgi-programs. Is there another
way to accomplish random images?

Clientside scripting, surely.

You uploading a different page every 5 minutes,
works as long as you don't fall asleep.

Serverside scripting, like ASP or PHP, if on your ISP.
[No, I don't want to have a look at your ISP's specs]

CGI-programming is not a primary method for such simple tasks, IMHO.
 
R

Ron Beitel

I would like to make a slide show using random images. The problem is my
host is 250.com, and they don't support cgi-programs. Is there another
way to accomplish random images?


Michael,

I have been working on a client-side script to automatically loop
though mulitple sets of images. Each slide is name with a sequential
number such as myImage1.jpg, myImage2.jpg myImage3.jpg. This
lets me us a "for loop" in the preload images into an Array, then I
can sequential step through the images by incrementing the counter
(currImg++). To randomly select the next image use....
currImg = Math.round(Math.random()*maxImg)


Here is a greatly simplified example of my script. I have stripped
off a lot of functions/buttoms, such First, Last, Prev, FrameRate and
the ability for the user to select different set of images.

Good Luck,
Ron Beitel


//***** SlideShow_Random.html ******

<html>
<head>

// ron beitel, 20 Dec 2004

<script language="JavaScript">

var delay = 500 // 1000 is 1 sec.
var currImg = 0 // index to the current image
var maxImg = 25 // default number of images (0 to 24)
var tid // timeout Id

var pix = new Array()

function preloadImgSet(max) {
for (i=0;i<max;i++) {
pix = new Image()
pix.src = "http://250.com/yourPath/image_" + i + ".jpg"
}

nxtSlide()
}

function nxtSlide() {
clearTimeout(tid)
currImg = Math.round(Math.random()*maxImg)
document.images.Slide.src = pix[currImg].src
tid = setTimeout('nxtSlide()', delay)
}

function loop() {
tid = setTimeout('nxtSlide()', delay)
}

function stop() {
clearTimeout(tid)
}

function next() {
currImg = Math.round(Math.random()*maxImg)
document.images.Slide.src = pix[currImg].src
}

</script>

</head>

<body onLoad="preloadImgSet(maxImg)">


<table border>
<tr><td colspan=2 align=center>
<h2>My Slide Show</h2>
<h3>Loop or Step Randomly</h3>
</td></tr>

<tr><td><table border="0" cellpadding="0" cellspacing="0">

<tr><td align="center">
<img src="" name='Slide'>
</td></tr>

<tr align="center"><td>
<form name="buttons">
<input type="button" value="Loop" onclick="loop()"/>
<input type="button" value="Stop" onclick="stop()"/>
<input type="button" value="Next" onclick="next()"/>
</form>
</td></tr>

</table></td></tr>

</table>




</body>
</html>
 
R

Ron Beitel

I would like to make a slide show using random images. The problem is my
host is 250.com, and they don't support cgi-programs. Is there another
way to accomplish random images?


Michael,

If you don't want to sequential number your filenames, change the

function preloadImgSet() {
pix[0] = new Image(); pix[0].src = "yourURL/imageA.jpg";
pix[1] = new Image(); pix[1].src = "yourURL/imageB.jpg";
pix[2] = new Image(); pix[2].src = "yourURL/imageC.jpg";

I didn't want to explicitly list the 500+ image files in my SlideShow.

Hope this helps,
Ron Beitel
 
R

Ron Beitel

JRS: In article <[email protected]>, dated
Tue, 21 Dec 2004 23:27:10, seen in Ron Beitel


Bad code - see FAQ.


Oops!

What Doctor John is either to busy or to arrogant to explain
is my use of Math.round() instead of Math.floor().

In my example, currImg gets a value between 0 and 25 inclusive.
But the Array of images holds 25 images, numbered 0 to 24.

0 <= Math.round(Math.random()*x) <= x
0 <= Math.floor(Math.random()*x) < x


Also apparently some browsers (i.e. Opera) don't implement
the random function and return [0 .. 1.0] instead of [0 .. 1.0).
Use a modulus 1 to gaurd against this. So change the lines...

currImg = Math.floor((Math.random() % 1) * maxImg)
 
D

Dr John Stockton

JRS: In article <[email protected]>, dated
Thu, 23 Dec 2004 02:10:58, seen in Ron
Beitel said:
Oops!

What Doctor John is either to busy or to arrogant to explain
is my use of Math.round() instead of Math.floor().

When you use Math.round in that manner, you are not choosing at random.
The first and last options are only half as probable as the others.
Choosing at random, which is what the OP asked for, and what your
comment claims that you are doing, means that all possibilities must be
substantially equi-probable.

Posting an answer without a sufficient understanding of a newsgroup's
regularly-posted FAQ shows either arrogance or ignorance.

The regulars, of course, all know why it is bad code. The others should
read the FAQ. That is why we have a FAQ.
 
R

ron

Sorry!

I should not have been so rude, but I was annoyed by your curt reply.

This was not that helpful. It is true, I just started visiting here
last week. And until I saw the footer on your reply, I did not know
where to find the FAQ. I must say they are very informative. I was
really impressed and bookmarked them. I am sure they will be a great
asset as I try to learn JavaScript.

Your explanation was much more helpful, and I immediately understood
the your point.
When you use Math.round in that manner, you are not choosing at random.
The first and last options are only half as probable as the others.
Choosing at random, which is what the OP asked for, and what your
comment claims that you are doing, means that all possibilities must be
substantially equi-probable.

Since, at first, I misunderstood your reasoning, perhaps my irritation
was justified. I thought your objection was the possibility of
Math.round(...) giving 25, while pix[25] did not exist. I was more
embarassed by this novice mistake, then the fact that the
nonsequential display of slides would not be statistically random.

Finally, I must object to the implication that only the masters may
contribute here. I alway found a seminar more stimulating then a
lecture. Certianly, this dialog has thought me two important
lessions--the proper way to find a random number and to keep my foot
out of my mouth. I only posted my solution, because a previous reply
indicated it was necessary to use some server-side technique. Since I
am currently writing a script to sequentially loop through a set of
images, I checked either Goodman's or Flaganan's book for
Math.random(), and quickly modified currImg++. In the future, I
promise to think, proof-read and test before posting. (That reminds
me, I need to take a red pen to that book. I'm sure the example used
Math.round())

Please accept my apology and thanks.

Ron
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,534
Members
45,007
Latest member
OrderFitnessKetoCapsules

Latest Threads

Top