# Random time interval to call function

**URL:** <https://forum.kirupa.com/t/random-time-interval-to-call-function/167059>\
**Category:** Uncategorized\
**Created:** [October 25, 2005, 3:25am UTC](https://forum.kirupa.com/t/random-time-interval-to-call-function/167059 "2005-10-25T03:25:48Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![juney](https://avatars.discourse-cdn.com/v4/letter/j/d26b3c/32.png) [@juney](https://forum.kirupa.com/u/juney)\
**Post date:** [October 25, 2005, 3:25am UTC](https://forum.kirupa.com/t/random-time-interval-to-call-function/167059/1 "2005-10-25T03:25:48Z")

</div>

what i’m trying to achieve is to be able to create a random time frame betw 1 to 8 seconds before it calls a movie clip from the library.

```auto

function layEgg(hens) {
 //set random time in sec
	var timeGap = Math.floor(Math.random()*(8+1))+1;
 
	hens.stop();
	
//converts to milliseconds by * 1000
	var randomness = setInterval(hens,"playAfterRandomInterval", timeGap*1000);
}

//after time interval it should play the frame and call the MC from function placeEgg
hens.playAfterRandomInterval = function() {
	trace("laying egg");
	hens.gotoAndPlay("layeggs");
	placeEgg(hens);
	clearInterval(randomness);
}

//these hens are the MC
this.onEnterFrame= function(){
	if(timeLeft >= 0){
layEgg(henMC1);
layEgg(henMC2);
layEgg(henMC3);
}else{
	delete this.onEnterFrame();
	}
}

	//hens. refers to the henMC
	//parent is where henMC is in
//this function calls the MC from library
	function placeEgg(hens){
	var depth:Number = hens._parent.getNextHighestDepth();
	var egg:MovieClip = hens._parent.attachMovie("eggMC", "eggMC_" + depth, depth);
	}

```

however the time interval is not working. i managed to trace the random timeGap. any idea?
