# Set interval question

**URL:** https://forum.kirupa.com/t/set-interval-question/265355
**Category:** Uncategorized
**Created:** [July 7, 2008, 11:54pm UTC](https://forum.kirupa.com/t/set-interval-question/265355 "2008-07-07T23:54:21Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![jibble](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/jibble/32/2265_2.png) [@jibble](https://forum.kirupa.com/u/jibble)
#### Post date: [July 7, 2008, 11:54pm UTC](https://forum.kirupa.com/t/set-interval-question/265355/1 "2008-07-07T23:54:21Z")

</div>

lets say i have a bunch of buttons and all the code i need is in the onRelease functions and the exact effect i wanted would be clicking the buttons every 5 seconds.

Basically what i want to do is use those onRelease functions on the buttons, in the button order(button1, button2, etc.) every 5 seconds, without clicking though. Is there a way i can tell it to use the onRelease functions? How would i go about setting this up?

Everything works perfectly i just want it to “auto-click” or “auto-call the onRelease’s” every 5 seconds. It would also be nice to be able to click the buttons at any given time too. So say the timer is on button4 and i click button2 and the timer starts from there.

my mind is telling me i should somehow be able to have something like the code below with a simple array or something maybe? But Murphy is telling me it just won’t be this easy 🙂

```auto
     myInterval = setInterval("button* onRelease function here", 5000);

```

---

<div class="post-metadata">

### Author: ![krilnon](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/krilnon/32/34_2.png) [@krilnon](https://forum.kirupa.com/u/krilnon)
#### Post date: [July 8, 2008, 12:05am UTC](https://forum.kirupa.com/t/set-interval-question/265355/2 "2008-07-08T00:05:56Z")

</div>

Something like this should work:

```auto
for(var i:Number = 0; i < 5; i++){
	var f:Function = function():Void {
		button[arguments.callee.i].onRelease();
	}
	f.i = i;
	myIntervals* = setInterval(f, 5000);
}

```

Also, I anticipated some other common errors that might have occurred in your code somewhere, which is why I included the loop structure and renamed _myInterval_ in the above code.

---

<div class="post-metadata">

### Author: ![jibble](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/jibble/32/2265_2.png) [@jibble](https://forum.kirupa.com/u/jibble)
#### Post date: [July 8, 2008, 3:19pm UTC](https://forum.kirupa.com/t/set-interval-question/265355/3 "2008-07-08T15:19:30Z")

</div>

arguments.callee refers to the function within button\* right?

makes sense. i can’t seem to get it working though. The buttons still work fine but they don’t switch with the time.

---

<div class="post-metadata">

### Author: ![krilnon](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/krilnon/32/34_2.png) [@krilnon](https://forum.kirupa.com/u/krilnon)
#### Post date: [July 8, 2008, 9:41pm UTC](https://forum.kirupa.com/t/set-interval-question/265355/4 "2008-07-08T21:41:25Z")

</div>

Hmm. Does this example make more sense?:

```auto
// make buttons
var buttons:Array = new Array();
for(var i:Number = 0; i < 5; i++){
	var b:MovieClip = createEmptyMovieClip('button' + i, i);
	buttons.push(b);
	b.clear();
	b.beginFill(0x4ba3fe);
	b.lineTo(75, 0);
	b.lineTo(75, 30);
	b.lineTo(0, 30);
	b.lineTo(0, 0);
	b.endFill();
	b._x = i * 100;
	b.index = i;
	b.onRelease = handler;
}

var interval:Number = setInterval(timer, 1000);
var curButtonIndex:Number = 0;

function timer():Void {
	buttons[curButtonIndex++ % buttons.length].onRelease();
}

function handler():Void {
	curButtonIndex = this.index + 1;
	// lazy alpha reset
	for(var i:Number = 0; i < 5; i++){
		buttons*._alpha = 100;
	}
	this._alpha = 50;
	clearInterval(interval);
	interval = setInterval(timer, 1000);
}

```

Running that should set up the buttons and handlers and everything.

---

<div class="post-metadata">

### Author: ![jibble](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/jibble/32/2265_2.png) [@jibble](https://forum.kirupa.com/u/jibble)
#### Post date: [July 9, 2008, 7:42pm UTC](https://forum.kirupa.com/t/set-interval-question/265355/5 "2008-07-09T19:42:33Z")

</div>

ooo…that works nicely!

A few things though, is it possible to instead of createEmptyMovieClip use the existing buttons? meaning, i have different onRelease code in each btn. i tried goofing with it and can’t seem to make that happen

---

<div class="post-metadata">

### Author: ![krilnon](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/krilnon/32/34_2.png) [@krilnon](https://forum.kirupa.com/u/krilnon)
#### Post date: [July 9, 2008, 11:14pm UTC](https://forum.kirupa.com/t/set-interval-question/265355/6 "2008-07-09T23:14:19Z")

</div>

Yes, it’s possible. The modification looks like this:

```auto
var buttons:Array = [btn1, btn2, btn3];
for(var i:Number = 0; i < buttons.length; i++){
    var b:MovieClip = buttons*
    b.index = i;
    b.onRelease = handler;
}

```

---

<div class="post-metadata">

### Author: ![jibble](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/jibble/32/2265_2.png) [@jibble](https://forum.kirupa.com/u/jibble)
#### Post date: [July 15, 2008, 4:44pm UTC](https://forum.kirupa.com/t/set-interval-question/265355/7 "2008-07-15T16:44:47Z")

</div>

thanks kril that works GREAT!

i am running into another issue however. Everything works perfect, but the setinterval starts at 5000 right, so the first buttons actions start at 5000. How would i modify this to make the first button start right away, and then every 5000 it does the actions with the above code. Other than that this is exactly what i was trying to do!

---

<div class="post-metadata">

### Author: ![krilnon](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/krilnon/32/34_2.png) [@krilnon](https://forum.kirupa.com/u/krilnon)
#### Post date: [July 15, 2008, 10:07pm UTC](https://forum.kirupa.com/t/set-interval-question/265355/8 "2008-07-15T22:07:17Z")

</div>

Modify the relevant lines to:

```auto
var interval:Number = 0;
var curButtonIndex:Number = 0;
handler.call(buttons[0]);

```
