|
|
Wakkos
Maniac (V) Mad Scientist
From: Azylum's Secret Lab Insane since: Oct 2000
|
posted 08-18-2001 22:00
Hi again, streight to the point:
i got this:
function showLayerNumber(number){
var layerNumToShow=number;
hideLayer(eval('"layer' + layerNumShowing+'"'));
showLayer(eval('"layer' + layerNumToShow+'"'));
layerNumShowing=layerNumToShow; }
It works with this:
<A HREF="javascript:showLayerNumber(2)">Shows the number layer 2</a>
is it possible to randomize the layer number every second?,
Maybe you'll need the complete script:
<SCRIPT LANGUAGE="JavaScript">
var totalLayersInLoop=31;
var layerNumShowing=1;
function init(){
if (navigator.appName == "Netscape") {
layerStyleRef="layer.";
layerRef="document.layers";
styleSwitch="";
}else{
layerStyleRef="layer.style.";
layerRef="document.all";
styleSwitch=".style";
} }
function showLayerNumber(number){
var layerNumToShow=number;
hideLayer(eval('"layer' + layerNumShowing+'"'));
showLayer(eval('"layer' + layerNumToShow+'"'));
layerNumShowing=layerNumToShow; }
function showPreviousLayer(){
var layerNumToShow=layerNumShowing-1;
if (layerNumToShow < 1){layerNumToShow=totalLayersInLoop;}
hideLayer(eval('"layer' + layerNumShowing+'"'));
showLayer(eval('"layer' + layerNumToShow+'"'));
layerNumShowing=layerNumToShow; }
function showNextLayer(){
var layerNumToShow=layerNumShowing+1;
if (layerNumToShow > totalLayersInLoop){layerNumToShow=1;}
hideLayer(eval('"layer' + layerNumShowing+'"'));
showLayer(eval('"layer' + layerNumToShow+'"'));
layerNumShowing=layerNumToShow; }
function showLayer(layerName){
eval(layerRef+'["'+layerName+'"]'+styleSwitch+'.visibility="visible"'); }
function hideLayer(layerName){
eval(layerRef+'["'+layerName+'"]'+styleSwitch+'.visibility="hidden"'); }
</SCRIPT>
Thanks!!
Wakk!
.-rotate script by Mr.Max
|
bitdamaged
Maniac (V) Mad Scientist
From: 100101010011 <-- right about here Insane since: Mar 2000
|
posted 08-18-2001 22:08
This will give you a number between 0 and 9. (good for arrays)
Changing Math.floor to Math.ceil should give you 1 through 10
You can change the 10 to any number (or a variable).
function getRandom() {
random = Math.floor(Math.random() *10)
setTimeout('getRandom()',1000)
}
<A HREF="javascript:showLayerNumber(random)">
:[ Computers let you make more mistakes faster than any other invention in human history, with the possible exceptions of handguns and tequila. ]:
[This message has been edited by bitdamaged (edited 08-18-2001).]
[This message has been edited by bitdamaged (edited 08-18-2001).]
|
Wakkos
Maniac (V) Mad Scientist
From: Azylum's Secret Lab Insane since: Oct 2000
|
posted 08-18-2001 22:30
'random' is undefined.....
Is that possible????
|
Slime
Lunatic (VI) Mad Scientist
From: Massachusetts, USA Insane since: Mar 2000
|
posted 08-19-2001 01:18
Yup, random has to be defined outside of the function for it to be defined anywhere outside of it. For instance
function myfunc()
{
random = 0;
alert(random); // "0"
}
alert(random); // "undefined"
compare that to:
var random;
function myfunc()
{
random = 0;
alert(random); //"0"
}
alert(random); // "0"
You may simply find it easier to do the following:
function RandomNumber()
{
var random = Math.floor(Math.random() * highest_number_allowed_plus_one);
return random;
}
<A HREF="javascript:showLayerNumber(RandomNumber())">Shows a random layer</a>
This method of doing it takes the return value of RandomNumber and passes it into showLayerNumber.
Note: You said you wanted it randomized *every second*. If you want the number to remain the same for a whole second, than you'll need to do it more like bitdamaged mentioned, using a setTimeout to continually randomize the number once a second. And if you want it to update *itself* once a second, without the link being clicked on more than once, you'll have to do that along with the randomization of the number, so that *it* happens once a second also.
|
bitdamaged
Maniac (V) Mad Scientist
From: 100101010011 <-- right about here Insane since: Mar 2000
|
posted 08-19-2001 03:41
yeah I forgot to start the loop.
just call getRandom() once and random will update once a second
:[ Computers let you make more mistakes faster than any other invention in human history, with the possible exceptions of handguns and tequila. ]:
|
Wakkos
Maniac (V) Mad Scientist
From: Azylum's Secret Lab Insane since: Oct 2000
|
posted 08-20-2001 23:01
This Worked!! Thanks!!
function RandomNumber()
{
var random = Math.floor(Math.random() * highest_number_allowed_plus_one);
return random;
}
<A HREF="javascript:showLayerNumber(RandomNumber())">Shows a random layer</a>
But, what happens if i want to show the layers when the pages loads? show them randomly every second...
Let me try to see if i can...
.-rotate script by Mr.Max
|
Wakkos
Maniac (V) Mad Scientist
From: Azylum's Secret Lab Insane since: Oct 2000
|
posted 08-21-2001 00:18
I made this:
function RandomNumber()
{
var random = Math.floor(Math.random() * 16);
return random;
}
var timerID = null;
function runLayers() {
timerID = setTimeout("showLayerNumber(RandomNumber())",1000)
}
<BODY onload="runLayers(RandomNumber);">
But the layers shows only once and stop, that's it!
Note:i have 16 layers...
.-rotate script by Mr.Max
[This message has been edited by Wakkos (edited 08-21-2001).]
|
bitdamaged
Maniac (V) Mad Scientist
From: 100101010011 <-- right about here Insane since: Mar 2000
|
posted 08-21-2001 00:35
I just like editing my posts
function runLayers() {
showLayerNumber(RandomNumber())",
timerID = setTimeout('runLayers()',1000)
}
Right now you are only calling ShowLayer once so you aren't getting a loop
Also you might notice this runs at different speeds on different boxes.
These kind of loops are pretty processor intensive so you'll get better performance on 400mhz+ boxes. You should preload all the images as well so to make things smoother.
:[ Computers let you make more mistakes faster than any other invention in human history, with the possible exceptions of handguns and tequila. ]:
[This message has been edited by bitdamaged (edited 08-21-2001).]
[This message has been edited by bitdamaged (edited 08-21-2001).]
[This message has been edited by bitdamaged (edited 08-21-2001).]
[This message has been edited by bitdamaged (edited 08-21-2001).]
|
Slime
Lunatic (VI) Mad Scientist
From: Massachusetts, USA Insane since: Mar 2000
|
posted 08-21-2001 01:47
Yeah, the point is, the setTimeout has to call the function that it's in, so that it sort of loops. The function switches the layers and then sets itself to run again in a second. Like Bitdamaged showed.
If you know for sure, though, that you never want the layers to *stop* switching, than you can simply replace
setTimeout
with
setInterval
which simply runs the specified code over and over at the specified interval.
|
Wakkos
Maniac (V) Mad Scientist
From: Azylum's Secret Lab Insane since: Oct 2000
|
posted 08-21-2001 02:08
I did it!! I did it! well, actually, you did it.....
That was my problem, i totally ignored the setInterval, so i was trying even to do a 'While' loop!
Thank you guys!!
BTW, another problem: When the layer is the "0" it says: document.all.layer0.style is null or not an objet, i think because the layer # 0 doen't exist, so, there's a way to avoid the "0" on the randomize?
[This message has been edited by Wakkos (edited 08-21-2001).]
|
bitdamaged
Maniac (V) Mad Scientist
From: 100101010011 <-- right about here Insane since: Mar 2000
|
posted 08-21-2001 03:05
I think
var random = Math.ceil(Math.random() * 16);
If you have layer1 through layer16 I'm pretty sure that should work.
Hey Slime with the Math.floor would that ever hit 16 (I don't think it does)
BTW Wakkos. for most languages in general it's best when you count something to start from 0
:[ Computers let you make more mistakes faster than any other invention in human history, with the possible exceptions of handguns and tequila. ]:
|
Wakkos
Maniac (V) Mad Scientist
From: Azylum's Secret Lab Insane since: Oct 2000
|
posted 08-21-2001 03:29
Roger
It works now (after one hour of looking)
I've heard about that, Bit, but i doesn't work with my layer counting from 0, let me recheck the script and try to star from 0....
from now on I am the Slime and Bit's Pupil...
[This message has been edited by Wakkos (edited 08-21-2001).]
|
Wakkos
Maniac (V) Mad Scientist
From: Azylum's Secret Lab Insane since: Oct 2000
|
posted 08-21-2001 06:11
Here is what i did!!!
Tell me what do you think!!!
.-rotate script by Mr.Max
|
bitdamaged
Maniac (V) Mad Scientist
From: 100101010011 <-- right about here Insane since: Mar 2000
|
posted 08-21-2001 06:44
very cool! I might speed it up just a little bit.
also a few of the images are broken (just looked at the code I bet those are the test.jpgs)
:[ Computers let you make more mistakes faster than any other invention in human history, with the possible exceptions of handguns and tequila. ]:
[This message has been edited by bitdamaged (edited 08-21-2001).]
|
Wakkos
Maniac (V) Mad Scientist
From: Azylum's Secret Lab Insane since: Oct 2000
|
posted 08-21-2001 15:26
Yes, i didn't upload those images, now it's time to make a nice background and maybe a menu the when the mouse is over, stop the animation and show only that layer, something like that! now it's time to make my job!!
Thanks Bitdamage!
|
Wakkos
Maniac (V) Mad Scientist
From: Azylum's Secret Lab Insane since: Oct 2000
|
posted 08-21-2001 16:01
Maybe i'm still being stupid, but, i have my RunLayers running, how do i stop it? i mean, i want on mouse over to show a specific layer, and stop the function while the mouse is over, and then, restore it again...
|
mr.maX
Maniac (V) Mad Scientist
From: Belgrade, Serbia Insane since: Sep 2000
|
posted 08-21-2001 18:23
You should use setTimeout to define timer (like in bitdamaged example) and then use "clearTimeout(timerID);" to stop it...
|
bitdamaged
Maniac (V) Mad Scientist
From: 100101010011 <-- right about here Insane since: Mar 2000
|
posted 08-21-2001 18:41
something like this
<a href="#" onmouseover="clearTimeout(timerID)" onmouseout="runLayers()">
:[ Computers let you make more mistakes faster than any other invention in human history, with the possible exceptions of handguns and tequila. ]:
|
Wakkos
Maniac (V) Mad Scientist
From: Azylum's Secret Lab Insane since: Oct 2000
|
posted 08-21-2001 18:48
I knew that something was missing in this post: Mr.Max Reply.
<a href="#" onmouseover="clearTimeout(timerID), showLayerNumber(3)" onmouseout="runLayers()">Shows Layer #3</a>
That worked pretty good!!!
Thanks again!!
|