473,800 Members | 2,418 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Randomizing images without preloading and repeats

I currently have a Javascript application that randomizes about 200
images. The problem is that the images preload, which causes the entire
site to not come up until all the images are loaded. I'd like to find a
Javascript application that can load images as they are randomly
chosen. In addition, I'm trying to figure out how to not display the
same image more than once; or at least until after the 200 have been
displayed, then create a new randomization of the 200 images.

Let me know if anyone knows how to do this or can point me to code that
is able to do what I'm looking for.

Thanks for your time!

Sep 25 '06 #1
2 1769
ut*****@gmail.c om wrote:
I currently have a Javascript application that randomizes about 200
images. The problem is that the images preload, which causes the entire
site to not come up until all the images are loaded. I'd like to find a
Javascript application that can load images as they are randomly
chosen. In addition, I'm trying to figure out how to not display the
same image more than once; or at least until after the 200 have been
displayed, then create a new randomization of the 200 images.

Let me know if anyone knows how to do this or can point me to code that
is able to do what I'm looking for.

Thanks for your time!
1. Provide the image source values in an array
2. Copy the array to use for the randomisation process
3. When required, randomly select one image source from the copy and
remove it from the array.
4. When there are no elements left in the copied array, copy the
original array back into it and start again.

e.g.

<script type="text/javascript">

var imageSrc = ['image1.jpg','i mage2.jpg','ima ge3.jpg',
'image4.jpg','i mage5.jpg','ima ge6.jpg'];
var imageSrc2 = [];;

function copyRandom(){
if (0 == imageSrc2.lengt h) {
imageSrc2 = imageSrc.concat ();
}
var randNum = (Math.random()* imageSrc2.lengt h) | 0;
return imageSrc2.splic e(randNum, 1);
}
</script>

<button
onclick="docume nt.getElementBy Id('xx').innerH TML = (copyRandom()); ">
Show random src
</button>
<div id="xx"></div>

You could even pre-select the next image and pre-load it so that you
only pre-load them one at a time. Provided there is a suitable pause
between the display of each image, the display of the next image will
be instantaneous.

You may get the same image displayed twice in a row if it is selected
as the last in one set and the first in the next set, but if you have
200 images in the array, the chance of that happening is pretty small.
If it is a problem, remember the last image used and if new image name
is the same, get another random number. That could be further
optimised by only checking when you've re-loaded the imageSrc2 array,
but I don't think it's necessary.
--
Rob

Sep 25 '06 #2
JRS: In article <11************ **********@k70g 2000cwa.googleg roups.com>,
dated Sun, 24 Sep 2006 20:04:01 remote, seen in
news:comp.lang. javascript, RobG <rg***@iinet.ne t.auposted :
>ut*****@gmail. com wrote:
>I currently have a Javascript application that randomizes about 200
images. The problem is that the images preload, which causes the entire
site to not come up until all the images are loaded. I'd like to find a
Javascript application that can load images as they are randomly
chosen. In addition, I'm trying to figure out how to not display the
same image more than once; or at least until after the 200 have been
displayed, then create a new randomization of the 200 images.
>1. Provide the image source values in an array
2. Copy the array to use for the randomisation process
3. When required, randomly select one image source from the copy and
remove it from the array.
4. When there are no elements left in the copied array, copy the
original array back into it and start again.
Or:

1. Ditto
2. Determine the size of the array (0..N-1).
3. Deal 0..N-1 in random order into a new array (see FAQ 4.22 links)
4. Use that array, re-dealing as often as needed, to index the images
array.

Note : with re-randomisation, some images will occur late in one cycle
and early in the next. To avoid that, either

A) Don't re-deal,
or
B) Make an empty second array. Deal as before from the first, move the
current picture to the end of the second array and (after the first 100
goes) move the first picture of the second array to the first array.
ISTM that a picture once shown will not be eligible again for 100 goes.

To be sure of avoiding pre-loading, do the above work with the numbers
of the images (say 1000 to 1999) and compose each name only when first
used.

--
© John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00 IE 4 ©
<URL:http://www.jibbering.c om/faq/>? JL/RC: FAQ of news:comp.lang. javascript
<URL:http://www.merlyn.demo n.co.uk/js-index.htmjscr maths, dates, sources.
<URL:http://www.merlyn.demo n.co.uk/TP/BP/Delphi/jscr/&c, FAQ items, links.
Sep 25 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

22
3140
by: Fabian | last post by:
var preload1 = new Image(); preload1.src = "/pic/yay.gif"; var preload2 = new Image(); preload2.src = "/pic/nay.gif"; The above is meant to preload image files, yes? Problem is, it doesnt seem to be doing so in practice. Any idea where Im going wrong? Could it be that things work differnetly when in an attached .js file? -- --
2
3999
by: Susanna | last post by:
Hi all, I'm using the following slideshow script that I found on the web to display changing images with a crossfade effect. Eventually I will be adding many more images to the slideshow. The thing is, I ALSO have to make it so the images will load randomly. I have looked at a number of scripts for random-loading slideshows, but I have to find a way to COMBINE this fading-image script (or, a different fading-image script, if...
2
1905
by: Julie | last post by:
Hi, I'm trying to change images on a website without reloading the whole page and use the following code to preload the images: var preloadFlag = false; function preloadImages() { if (document.images) { pic_moon2_click = newImage("images/moonpic2.jpg"); pic_moon3_click = newImage("images/moonpic3.jpg");
6
1589
by: michaaal | last post by:
Is this the correct way to preload an image...? Var Image1 = new Image() ....And then when I'm ready to use the image I can do this...? Button1.src=Image1.src ....Or am I just telling Button1 to use the same source path as Image1?
5
3520
by: hoolie | last post by:
I'm preloading images with the standard code: default1 = new Image(); default1.src = "images/image1.gif"; changed1 = new Image(); changed1.src = "images/image1-on.gif"; default2 = new Image(); default2.src = "images/image2.gif"; changed2 = new Image(); changed2.src = "images/image2-on.gif"; And this is my rollover code: var iName="";
10
1882
by: cosmic foo | last post by:
I have a page with about 100 images of about 10k each. where a typical img tag looks like this, <img src="item001.jpg" height="70" alt="" onMouseOver="showPic(this)" border="0"> Not all the images need to be viewed at once, depending on what category is selected, only about 10 images are seen at any one time. I hide and show categories of images by setting a div container display:none/block or visibility:hidden/visible. Even though the...
2
1859
by: windandwaves | last post by:
Hi Gurus Preloading images has got to be JS 101. However, it does not seem to be working. Here is the function that I am using. I added the alerts to make sure it is working and all the right alerts show up, yet when I do my mouseover, it still takes about a second (only the first time) to load the image (thumbnail). Am I missing anything?
3
9514
by: littleark | last post by:
Hi everybody, I have a typical javascript images preloader, it works fine both on Firefox and on IE in my intranet (local server). It works fine on the Internet (remote server) in IE. In Firefox it stops before loading every image. Does image.onload function have some problems with little images?
7
2001
by: Keith Hughitt | last post by:
Hi all, I am having trouble preloading images in a javascript application, and was wondering if anyone had any suggestions. Basically I have a bunch of images stored in a database as BLOBs. At any given point in time a subset of those images is displayed on- screen. At certains times I want to swap out those on screen with new ones from the database, and do some as seamlessly as possible. So what I've tried to do is first create Image...
0
9690
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
1
10253
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10033
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9085
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7576
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6811
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5471
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4149
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3764
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.