Re: Need Help Creating an Image Slideshow
- From: Thomas 'PointedEars' Lahn <PointedEars@xxxxxx>
- Date: Sun, 21 Jun 2009 17:39:00 +0200
Bart Van der Donck wrote:
Madcap wrote:
I want to experiment with a new feature for my web app. The feature
is basically a "slideshow" of pictures with each picture loading in
the same frame after a delay of some seconds. So, when the user opens
the page/frame it will...
1. Load multiple images from web server (preload all)
2. Display first image
3. Wait xx seconds (different interval for each picture)
4. Load next image
5. Repeat
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Slideshow</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<script type="text/javascript">
var interval = 1.5; // in seconds
var t = 0;
Unnecessary global variables.
var f = new Array();
f[0] = new Image();
Untested construction of host object.
f[0].src = 'http://216.92.6.95/app_pics/large/kCAm14d1JNRM7vn.jpg';
f[1] = new Image();
f[1].src = 'http://216.92.6.95/app_pics/large/YQk8ePvHibk19W6.jpg';
f[2] = new Image();
f[2].src = 'http://216.92.6.95/app_pics/large/H3rmiFJw3MheUaC.jpg';
Unnecessary, harder-to-maintain assignments.
var f = [
"http://216.92.6.95/app_pics/large/kCAm14d1JNRM7vn.jpg",
"http://216.92.6.95/app_pics/large/YQk8ePvHibk19W6.jpg",
"http://216.92.6.95/app_pics/large/H3rmiFJw3MheUaC.jpg"
];
// or: jsx.object.isMethod(jsx.global, "Image"))
var t = typeof Image;
if (/^\s*unknown\s*/i.test(t)
|| /^\s*(function|object)\s*$/i.test(t) && Image)
{
var imageFactory = function(uri) {
var img = new Image();
img.src = uri;
return img;
};
for (var i = f.length; i--;)
{
f[i] = imageFactory(f[i]);
}
}
function refr() {
++t;
if (t >= f.length) t=0;
Inefficient operation; as you initialize with 0, and increment by 1,
only `>' is needed. However, I would not use a global variable here:
function refr(img, intv, t)
{
var me = arguments.callee;
if (typeof me.target == "undefined")
{
// or: !jsx.object.getFeature("document", "images", "pic")
if (typeof document == "undefined"
|| typeof document.images == "undefined"
|| typeof document.images[imgName] == "undefined"
|| typeof (me.target = document.images[imgName]).src
== "undefined")
{
return false;
}
}
if (typeof t == "undefined") t = -1;
++t;
img.src = (typeof f[t].src != "undefined" ? f[t].src : f[t]);
if (typeof window != "undefined"
&& jsx.object.isMethod(window, "setTimeout"))
{
window.setTimeout(function() { me(img, intv, t); }, intv * 1000);
}
}
In fact, all slides could be defined as properties of the animation method
so as to minimize the use of global variables. Then again, one would
consider using a slideshow object in the first place.
var slideshow = {
slides: [
{uri: ..., specialDelay: 0.5},
...
],
start: 0,
delay: 1.0,
run: function() {
// this.slides, this.start, this.current, this.delay
}
};
<body onload="slideshow.run('pic', 1.5)">
document.images['pic'].src = f[t].src;
Untested property access (Reference Worm).
setTimeout('refr()', interval * 1000);
Untested method call, relying on more error-prone scope chain resolution.
}
</script>
</head>
<body onLoad="setTimeout('refr()', interval * 1000);">
<body onload="refr('pic', 1.5);">
PointedEars
.
- Follow-Ups:
- Re: Need Help Creating an Image Slideshow
- From: Bart Van der Donck
- Re: Need Help Creating an Image Slideshow
- From: Thomas 'PointedEars' Lahn
- Re: Need Help Creating an Image Slideshow
- References:
- Need Help Creating an Image Slideshow
- From: Madcap
- Re: Need Help Creating an Image Slideshow
- From: Bart Van der Donck
- Need Help Creating an Image Slideshow
- Prev by Date: Re: Why can't I construct an XMLHttpRequest?
- Next by Date: Re: Why can't I construct an XMLHttpRequest?
- Previous by thread: Re: Need Help Creating an Image Slideshow
- Next by thread: Re: Need Help Creating an Image Slideshow
- Index(es):
Relevant Pages
|