# Random fade in/out in image library

**URL:** <https://forum.kirupa.com/t/random-fade-in-out-in-image-library/308268>\
**Category:** Uncategorized\
**Created:** [April 20, 2010, 12:35pm UTC](https://forum.kirupa.com/t/random-fade-in-out-in-image-library/308268 "2010-04-20T12:35:36Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![jendam7](https://avatars.discourse-cdn.com/v4/letter/j/aeb1de/32.png) [@jendam7](https://forum.kirupa.com/u/jendam7)\
**Post date:** [April 20, 2010, 12:35pm UTC](https://forum.kirupa.com/t/random-fade-in-out-in-image-library/308268/1 "2010-04-20T12:35:36Z")

</div>

hi guys,

i have this code and cant work out how to make the fade in time be random, say between 1 and 10 seconds for instance.

thanks, its been a while since i played with AS.

//: SETUP VARIABLES  
var maxVal = 4; // max number of fotos  
var oldVar = 0; // keep track of previous random number  
var newVar = 0; // used to load the next image  
var si = 0; // interval variable  
var desc = []; // create our description array  
desc[0] = ‘Antigua’; // descriptiong for foto0.jpg  
desc[1] = ‘Flag’s Emblem’; // descriptiong for foto1.jpg  
desc[2] = ‘Village’ // descriptiong for foto2.jpg  
desc[3] = ‘Mayan Girl’ // descriptiong for foto3.jpg  
desc[4] = ‘Lake Atitlan’ // descriptiong for foto4.jpg  
//: LOAD THE NEXT IAMGE  
function getImage() {  
newVar = Math.floor(Math.random() \* maxVal); // get random number  
if (newVar == oldVar) { // if number = old number…  
getImage(); // get a number  
} else { // else  
oldVar = newVar; // set old to new number  
container\_mc.loadMovie (“fotos/foto” + newVar + “.jpg”); // load the next image  
container\_mc.\_alpha = 0; // set its alpha to 0  
this.onEnterFrame = function () { // create loop  
if (container\_mc.\_width \> 0) { // check that the image has been loaded  
container\_mc.\_x = Stage.width / 2 - container\_mc.\_width / 2; // center movieclips to stage  
container\_mc.\_y = Stage.height / 2 - container\_mc.\_height / 2 // center movieclip to stage  
container\_mc.onEnterFrame = fadeIn; // start fading out  
desc\_txt.text = desc[newVar]; // display the images description  
delete this.onEnterFrame; // delete loop  
}  
}  
}  
}  
//: FADE IN THE CURRENT MOVIECLIP  
function fadeIn () {  
if (this.\_alpha \<= 100) { // if the movieclips alpha is greater than 0  
this.\_alpha += 5; // reduce alpha by 5  
} else { // else  
this.\_alpha = 100; // reduce alpha to 0  
delete this.onEnterFrame; // delete handler  
si = setInterval(fadeOut, 2000); // after 2 seconds, fade out the movieclip  
}  
}  
//: FADE OUT THE CURRENT MOVIECLIP  
function fadeOut () {  
clearInterval(si); // clear the interval variable  
container\_mc.onEnterFrame = function() { // create loop to fade out  
if (this.\_alpha \>= 0) { // if the movieclips alpha is greater than 0  
this.\_alpha -= 5; // reduce alpha by 5  
} else { // else  
this.\_alpha = 0; // reduce alpha to 0  
delete this.onEnterFrame; // delete handler  
getImage(); // load the next image  
}  
}  
}  
// load first image  
getImage();
