# \[MX\] setInterval or getTimer?

**URL:** <https://forum.kirupa.com/t/mx-setinterval-or-gettimer/23275>\
**Category:** flash\
**Created:** [June 15, 2003, 6:36pm UTC](https://forum.kirupa.com/t/mx-setinterval-or-gettimer/23275 "2003-06-15T18:36:37Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![Felipe\_Bastos](https://avatars.discourse-cdn.com/v4/letter/f/f05b48/32.png) [@Felipe\_Bastos](https://forum.kirupa.com/u/Felipe_Bastos)\
**Post date:** [June 15, 2003, 6:36pm UTC](https://forum.kirupa.com/t/mx-setinterval-or-gettimer/23275/1 "2003-06-15T18:36:37Z")

</div>

I`m building a photo slide projector that calls a function using setInterval. But after the tenth call (about after 50 sec) the flash player stops working and crashes.  
On the first frame of the main timeline I set the following code:

```auto
// comando para fullscreen
fscommand("fullscreen", "true");
fscommand("allowscale", "false");

// funcao que attacha mc_loadimg e carrega img
function loadimg () {
	foto1 = Math.floor(random(48));
	foto2 = Math.floor(random(48));
	foto3 = Math.floor(random(48));
	foto4 = Math.floor(random(48));
	loadMovie(foto4 + ".jpg", "_root.foto1");
	loadMovie(foto2 + ".jpg", "_root.foto2");
	loadMovie(foto2 + ".jpg", "_root.foto3");
	loadMovie(foto4 + ".jpg", "_root.foto4");
	setInterval(_root.loadimg, 5000);
	updateAfterEvent();
}

```

On a movieclip on the stage:

```auto
onClipEvent(load) {
	_root.loadimg ();
}

```

[color=blue]Would it be better if I use a getTimer and if statements?[/color]  
Which one is more apropriate for this ocasion?

thanks

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [June 15, 2003, 7:14pm UTC](https://forum.kirupa.com/t/mx-setinterval-or-gettimer/23275/2 "2003-06-15T19:14:44Z")

</div>

I like setInterval better than getTimer(), getTimer() is a pain in my … beeep…lol. Well that depends on the situation, in this case I think setInterval is better. I can’t see why your coding freezes, but try this anyway…

Remove the onClipEvent(load) from your movie clip on the stage and have this on the frame…

[AS]// comando para fullscreen  
fscommand(“fullscreen”, “true”);  
fscommand(“allowscale”, “false”);

// funcao que attacha mc\_loadimg e carrega img  
function loadimg () {  
f2 = Math.floor(random(48));  
f4 = Math.floor(random(48));  
loadMovie(f4 + “.jpg”, “\_root.foto1”);  
loadMovie(f2 + “.jpg”, “\_root.foto2”);  
loadMovie(f2 + “.jpg”, “\_root.foto3”);  
loadMovie(f4 + “.jpg”, “\_root.foto4”);  
}  
//run the function initially  
loadimg();  
//use setInterval to run it every 5 seconds after that  
setInterval(loadimg, 5000);[/AS]

Set interval works every 5 seconds no matter what, you don’t need to reinitiate it every 5 seconds when the function is run, that isn’t very efficient, so what I did instead was moved the setInterval outside of the function you are calling. This of course would make the viewer have to wait 5 seconds to see the function in the first place, so right before initiating the setInterval I called the function to run it when that frame is hit. That way you get the movies loaded right off the bat AND every 5 seconds after that.

As for inside the function itself I removed the updateAfterEvent(), I saw no need for that in this script since you don’t need anything updated after an event, it will do it anyway. I also don’t think it’s wise to have the same variable names as instance names, it is confusing the coder and maybe to flash, so I changed those. And last put not least, I noticed your script you only used the foto2 and foto4 variables, so I removed foto1 and foto3, no need to run them if they aren’t getting used, you know how to put them back if you need them 😉

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [June 15, 2003, 7:24pm UTC](https://forum.kirupa.com/t/mx-setinterval-or-gettimer/23275/3 "2003-06-15T19:24:53Z")

</div>

i guess it crashes because everytime the function is called, a new interval is defined, so don’t set the interval in the function itself.

i think you should use something like this instead:

```auto
onClipEvent (load) {
imgInterval = setInterval(loadimg, 5000);
}

```

and by the way…

```auto
Math.floor(random(48));

```

returns the same value than just

```auto
random(48);

```

you’d only need to round the number if you use the Math.random method.

```auto
Math.floor(Math.random()*48));

```

=)

**edit.** oops… i should reload the page before posting. 😛  
sorry, lost. :-\

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [June 15, 2003, 7:26pm UTC](https://forum.kirupa.com/t/mx-setinterval-or-gettimer/23275/4 "2003-06-15T19:26:59Z")

</div>

Good catch on the random(48) thing kax, I saw Math.floor and my head automatically pictured Math.random() 😛

But at least we have the same theory on why it crashes, the setInterval defined in the function.

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [June 15, 2003, 8:19pm UTC](https://forum.kirupa.com/t/mx-setinterval-or-gettimer/23275/5 "2003-06-15T20:19:17Z")

</div>

Thanks for that! 🙂  
It`s working better now.  
But sometimes no image is loaded. What might be happening?  
All the images have their number set from foto1.jpg to foto48.jpg.

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [June 15, 2003, 8:29pm UTC](https://forum.kirupa.com/t/mx-setinterval-or-gettimer/23275/6 "2003-06-15T20:29:36Z")

</div>

I really want to learn how to make this code work in a different way.  
Like this: the image is loaded in mc01 and then mc01 is triplicated into those positions and set rotation, alpha, color change. Is it possible to reflect the image as it is possible in photoshop (transform - reflect - horizontal or vertical)?  
thanks again! :thumb:

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [June 15, 2003, 8:29pm UTC](https://forum.kirupa.com/t/mx-setinterval-or-gettimer/23275/7 "2003-06-15T20:29:40Z")

</div>

that’s because random(48) returns a number between 0 and 47.  
so does Math.floor(Math.random()\*48).

```auto
random(48)+1;
// or
Math.floor(Math.random()*48)+1;

```

😉

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [June 15, 2003, 8:31pm UTC](https://forum.kirupa.com/t/mx-setinterval-or-gettimer/23275/8 "2003-06-15T20:31:23Z")

</div>

trace random, should give you values from 0 to 47 😉

edit. oops… i should reload the page before posting.  
sorry, kax :sigh: …same goes for me; lol

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [June 15, 2003, 8:33pm UTC](https://forum.kirupa.com/t/mx-setinterval-or-gettimer/23275/9 "2003-06-15T20:33:18Z")

</div>

well you could flip the image by setting its \_xscale/\_yscale to -100 (there’s a recent thread about that).

but when you duplicate the movieclip, you must load the .jpg into each movieclip. :-\

**edit.** 😛  
it’s ok, eyez. 😉

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [June 15, 2003, 8:34pm UTC](https://forum.kirupa.com/t/mx-setinterval-or-gettimer/23275/10 "2003-06-15T20:34:17Z")

</div>

But this way image number 01 will never be loaded!!  
What about?

```auto
 random(49);

```

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [June 15, 2003, 8:36pm UTC](https://forum.kirupa.com/t/mx-setinterval-or-gettimer/23275/11 "2003-06-15T20:36:01Z")

</div>

it will, 0+1 =1!  
random 49 will still return 0 sometimes, do you have a 0.jpg?

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [June 15, 2003, 8:42pm UTC](https://forum.kirupa.com/t/mx-setinterval-or-gettimer/23275/12 "2003-06-15T20:42:49Z")

</div>

Oh sure!  
Yes I have a 0.jpg!

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [June 15, 2003, 8:50pm UTC](https://forum.kirupa.com/t/mx-setinterval-or-gettimer/23275/13 "2003-06-15T20:50:36Z")

</div>

Hey Kax.  
I didnt find the recent thread u told me about.  
Could u post its url here?  
thanks

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [June 15, 2003, 8:56pm UTC](https://forum.kirupa.com/t/mx-setinterval-or-gettimer/23275/14 "2003-06-15T20:56:37Z")

</div>

hmmm… i thought there would be more info about it.  
there are just a few snippets of code.

[http://www.kirupaforum.com/forums/showthread.php?s=&threadid=25877](http://www.kirupaforum.com/forums/showthread.php?s=&threadid=25877)
