Jump to content

Javascript image replacements


Farsue

Recommended Posts

I don't know if it's possible to do, but can you using javascript tell it to replace the current image by a new one every 2 seconds or any amount of time? Almost like a side show or frames for a movie. The code I have so far is listed below. Any help would be greatly appreciated, kind of new.

<html><head><script type="text/javascript">function timedImage(){var t1=setTimeout("document.b1.src ="novas(base)web.jpg",2000)var t2=setTimeout("document.b1.src ="novas(secondframe)web.jpg",4000)var t3=setTimeout("document.b1.src ="novas(thirdframe)web.jpg",6000)}</script></head><body><form><input type="button" value="dance" onClick="timedImage()"><img border="0" alt="Visit W3Schools!" src="novas(base)web.jpg" name="b1" width="800" height="800" /></form></body></html>

~Farsue

Link to comment
Share on other sites

<html><head><script type="text/javascript">var i=0;arr = ['novas(base)web.jpg', 'novas(base)web.jpg', 'novas(base)web.jpg'];function changeImg(){    document.b1.src = arr[i++%arr.length];   setTimeout(changeImg,2000);}</script></head><body onload="changeImg()"><form><input type="button" value="dance" onClick="timedImage()"><img border="0" alt="Visit W3Schools!" src="novas(base)web.jpg" name="b1" width="800" height="800" /></form></body></html>

try that....setInterval is every 2 seconds, setTimeout is 2 seconds later but only once. And don't forget about nesting quotes and closing parenthesis

Link to comment
Share on other sites

It still says error on the page, don't know. I checked my images they're alright. Just out of curiosity what are you doing with the array and why is i being divided by it? sorry I'm a complete newbie.EDIT:never mind you're a genius it works thanks. I'd still like to know what the array does if you don't mind.

Link to comment
Share on other sites

The array is just so you can declare all the images at one location so it makes changing things and looping easier. And the array isn't being divided by i it's being "modulused". The percent sign means to divide the left number by the right and return the remainder. It's basically used to limit values to a certain number, in this case 3.

Link to comment
Share on other sites

In compiled languages it's called buffer overflow. And array is a series of memory locations mapped by the processor and allocated to your process. When the language does its parsing and creates instructions for the process it tells it which element to get in that segment, but if it tells it to get something outside that segment you can get very strange results.In javascript, it will simply result in an error. Since arrays are indexed starting at 0 the length represents the last index + 1 so you never want to reach the array length or you'll be accessing a null element (well, actually it's worse than null). However that is often used to add elements to an array in javascript. Like this

arr[arr.length] = "new value";

Link to comment
Share on other sites

so I think I'm a re re or something and sorry to take up your time, but in the code

document.b1.src = arr[i++%arr.length];

wouldn't "i" eventually equal an amount that was greater than 3 (think there's three in the array) because it's an endless loop? What's stopping it from becoming 4 and then giving an answer of 4/3 or 5/3, you'd get a decimal greater than 1 then and no value for an image.appreciating the help~farsue

Link to comment
Share on other sites

Hah, it's no problem.And what I was talking about with the modulus is what the percent sign is. So when I say 4%3 it doesn't mean 4/3 it means divide 4 by 3 and return the remainder so even if it becomes infinitely large the remainder will never be greater than or equal to 3.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...