/    Sign up×
Community /Pin to ProfileBookmark

Time based image rotating with huge amount of images

Hello.

Im looking for help to make a site with image wich would change based
on seconds and minutes of an hour.

Image (size about 500×280) should change every second
(or at least every 5 sec).

Example: If I enter the site at 15:00.07, first image i would see would be Image 0007 which changes after a second to 0008 and so on.

If entering the site 17:59.59 Image would number 3600 and next would be 0001.

I hope you understud. Ask is problems to understand.

I really really appreciate any kind of help.

vicward

to post a comment
JavaScript

18 Comments(s)

Copy linkTweet thisAlerts:
@TheBearMayApr 12.2010 — This should get you fairly close:

<i>
</i>displayImg();
function displayImg() {
var now = new Date();
var baseDate = new Date(new Date().getFullYear(), new Date().getMonth(), new Date().getDate(), 15, 00, 00, 00);
var imgNum = parseInt(Math.abs(baseDate-now)/1000);
while (imgNum &gt; 3600) {
imgNum-=3600;
}
if (imgNum &lt; 10) imgNumPadded = "000"+imgNum;
else if (imgNum &lt; 100) imgNumPadded = "00"+imgNum;
else if (imgNum &lt; 1000) imgNumPadded = "0"+imgNum;
document.getElementById("imageID").src="Image"+imgNumPadded; // replace imageID with the id of the image tag
setTimeout("displayImg()",1000);
}
Copy linkTweet thisAlerts:
@tirnaApr 13.2010 — Another way of doing it is below.

I haven't tested the code so I hope there are no typos or bugs, but you should be able to see the general method I am using.

[code=php]
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<script type="text/javascript">
<!--

var imgNum;
var imgPaths = new Array();
var myImages = new Array();
var timer;

// I assume your 3600 images are named pic1.jpg to pic3600.jpg
//load the 3600 image paths
for(var i=1; i<=3600; i=i+1) {
imgPaths[i] = 'pic'+i+'.jpg';
}

//preload the images
for(var i=1; i<=3600; i=i+1) {
myImages[i] = new Image();
myImages[i].src = imgPaths[i];
}

//load the initial image
function loadImage() {
var currDate = new Date();
var currMin = currDate.getMinutes();
var currSec = currDate.getSeconds();
imgNum = (currMin*60)+currSec;
document.getElementById("myImg").src = myImages[imgNum].src;
}

//function to swap the image
function swapImage() {
imgNum = imgNum + 1;
if(imgNum > 3600) imgNum = 1;
document.getElementById("myImg").src = myImages[imgNum].src;
}
//-->
</script>
</head>

<body onload="loadImage(); setInterval('timer=swapImage()',1000);">

<div>
<img id="myImg" src="" alt="" />
</div>

</body>
</html>
[/code]
Copy linkTweet thisAlerts:
@mrvictorwardauthorApr 13.2010 — Many thanks for quick replies.

I will be looking into this hopefully today evening.

I will let you know how Im doing with my page.

vicward
Copy linkTweet thisAlerts:
@mrvictorwardauthorApr 14.2010 — Ok

On my computer its running nice and smooth.

But I cant get this thing working on net.

My testing address: http://oefa002.110mb.com/TESTER/Koodi.php

Loading time must be amazingly large...

Any ideas how to get this thing working?

Thanks again,


Vicward
Copy linkTweet thisAlerts:
@mrvictorwardauthorApr 14.2010 — And to add... if you see above site. There is 3600 images but only two different ones rotating following way:

(image1 > image2 > image1 > image1 > image2 >image2 and to begin...)
Copy linkTweet thisAlerts:
@tirnaApr 15.2010 — [B]There is no way[/B] I am going to click on your link with 3600 images unless I was sure the images are very tiny.

What is the total size (in M? of your images?

Even if they are 25kb each, you are asking your visitors to download up to 90MB in total just for the images.

A better option could be for you to:

1) open the web page on the server

2) right-click the page and "view source"

3) save the source as a text file

4) post the text file here
Copy linkTweet thisAlerts:
@mrvictorwardauthorApr 15.2010 — No need of anykind of clicking. Its rolling automaticly when they get loaded, but problem is excatly size of images 50mb. It takes too long to load.

Well maybe code should just preload the first 30 images based on the current second of the hour? And after that second by second? Could that work or would it go wrong after those 30 seconds?

Any help and ideas?

Current Code below.

<head>

<title></title>

<script type="text/javascript">

<!--

var imgNum;

var imgPaths = new Array();

var myImages = new Array();

var timer;

// I assume your 3600 images are named pic1.jpg to pic3600.jpg

//load the 3600 image paths

for(var i=1; i<=3600; i=i+1) {

imgPaths[i] = 'pic'+i+'.gif';

}



//preload the images

for(var i=1; i<=3600; i=i+1) {

myImages[i] = new Image();

myImages[i].src = imgPaths[i];

}



//load the initial image

function loadImage() {

var currDate = new Date();

var currMin = currDate.getMinutes();

var currSec = currDate.getSeconds();

imgNum = (currMin*60)+currSec;

document.getElementById("myImg").src = myImages[imgNum].src;

}



//function to swap the image

function swapImage() {

imgNum = imgNum + 1;

if(imgNum > 3600) imgNum = 1;

document.getElementById("myImg").src = myImages[imgNum].src;

}

//-->

</script>

</head>
Copy linkTweet thisAlerts:
@tirnaApr 15.2010 — I assume you have optimised the images for the web in terms of filesize in Photoshop or some other omage editing software.

Maybe don't preload the images and just let the browser down load each image from the server as it needs it during the rotation.

Or maybe cut down the number of images to around 10-20 and start with a random one on initial page load and then rotate the rest every few seconds or so.

Anyway just food for thought...
Copy linkTweet thisAlerts:
@mrvictorwardauthorApr 15.2010 — Yep. Images are optimized.

How to not preload and load as time goes?

[Simply cannot cut down number of images or do any random rotation.

The whole point is to get this working as time based and with huge amount of images]
Copy linkTweet thisAlerts:
@tirnaApr 15.2010 — Leave out:

[code=php]
var myImages = new Array();

//preload the images
for(var i=1; i<=3600; i=i+1) {
myImages[i] = new Image();
myImages[i].src = imgPaths[i];
}


[/code]


and change:

[code=php]document.getElementById("myImg").src = myImages[imgNum].src;[/code]

to

[CODE]document.getElementById("myImg").src = imgPaths[imgNum];[/CODE]

[FONT=Tahoma][SIZE=3][COLOR=black]in both loadImage() and swapImage()[/COLOR][/SIZE][/FONT]

[FONT=Tahoma][SIZE=3][COLOR=black][/COLOR][/SIZE][/FONT]

[FONT=Courier New][COLOR=#007700][FONT=Tahoma][SIZE=3][COLOR=black]and then cross all your fingers and toes and hope that it works because I haven't tested this code[/COLOR][/SIZE][/FONT] :eek:[/COLOR][/FONT]
Copy linkTweet thisAlerts:
@mrvictorwardauthorApr 15.2010 — Oh.... Million thanks again, even thought no luck.

I so hoped that it would have worked, but no.

Same thing again. On harddrive no problem.

On net no luck.

I am becaming desperate...
Copy linkTweet thisAlerts:
@tirnaApr 15.2010 — Since it works on your local drive then it should work on the www.

The one issue I can think of is that the interval in setInterval(), set to 1000 (1sec) in the code, is probably too short to work on the www.

It will work locally because the browser is getting the images straight from the local drive and not downloading from some remote server.

Maybe try changing the interval to 5000 or 10000 which should then give the browser ample time to complete downloading an image before it calls for the next one.

Currently, with a 1 sec interval maybe the browser hasn't finished downloading an image before calling for the next one and that could be why it's not working on the www.
Copy linkTweet thisAlerts:
@criterion9Apr 16.2010 — In Flash I always start the timer after 2 conditions are true for the current image

1) the image has been completely downloaded

- & -

2) the image has been transitioned for display (i.e. faded in, etc).

Immediately following the display of the image I always start loading the next image, though in this case you may want to load the first x images ahead. If it is imperative that the images are only displayed for 1 second you have a couple options.

1) preload all the images before you start displaying them (this will cause a major delay before the loading begins)

- or -

2) Display a "loading" graphic or text whenever your loading buffer (preloading) catches up to your displayed image.

As for it working on the local HD but not online there is a simple reason why. The browser already has a local copy of the files when displaying on the local computer so it only needs to load them from the HD. When displayed on the internet the browser has to download the images first which will take some time for the sheer number of images you are displaying.

If I may ask, is this some sort of animation or just a slide show?
Copy linkTweet thisAlerts:
@tirnaApr 16.2010 —  If it is imperative that the images are only displayed for 1 second you have a couple options.

1) preload all the images before you start displaying them (this will cause a major delay before the loading begins)
[/quote]


I thought this was the original solution suggested and in subsequent posts it was decided this takes too long because of the total file size.



As for it working on the local HD but not online there is a simple reason why. The browser already has a local copy of the files when displaying on the local computer so it only needs to load them from the HD. When displayed on the internet the browser has to download the images first which will take some time for the sheer number of images you are displaying.
[/quote]


Is this not the same as:

It will work locally because the browser is getting the images straight from the local drive and not downloading from some remote server[/QUOTE]

that was posted earlier?


Or am I missing a point you are trying to make? or am I just seeing double ?
Copy linkTweet thisAlerts:
@criterion9Apr 16.2010 — @tirna:

I was reiterating because the OP asked a rephrased question again which usually indicates there might have been some confusion with the previous answers.

@OP:

Another thought on this is that you can time the first few downloads to determine the average number of images you'll need to keep loaded in advance of the currently displayed image based off the connection and download times on the client machine.
Copy linkTweet thisAlerts:
@tirnaApr 16.2010 — ok thanks ? at least I know I was seeing double and not missed a point you were making.
Copy linkTweet thisAlerts:
@mrvictorwardauthorApr 16.2010 — 1 second is the key here. 2 seconds and whole system is not the same anymore.

Displaying a "loading" graphic or text is not an option.

It is kind of some sort of animation.

I know there is a way for this but it needs to be discovered.
Copy linkTweet thisAlerts:
@mrvictorwardauthorApr 16.2010 — "Another thought on this is that you can time the first few downloads to determine the average number of images you'll need to keep loaded in advance of the currently displayed image based off the connection and download times on the client machine."

Yep. Something like this could maybe work. I dont know how to do it but I would be happy to test it.
×

Success!

Help @mrvictorward spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...