# Does setInterval lag?

**URL:** <https://forum.kirupa.com/t/does-setinterval-lag/26144>\
**Category:** flash\
**Created:** [July 20, 2003, 4:46am UTC](https://forum.kirupa.com/t/does-setinterval-lag/26144 "2003-07-20T04:46:47Z")\
**Posts on this page:** 18\
**Page:** 1

<div class="post-metadata">

**Author:** ![iLikePie](https://avatars.discourse-cdn.com/v4/letter/i/85f322/32.png) [@iLikePie](https://forum.kirupa.com/u/iLikePie)\
**Post date:** [July 20, 2003, 4:46am UTC](https://forum.kirupa.com/t/does-setinterval-lag/26144/1 "2003-07-20T04:46:47Z")

</div>

hi,

i’m making a little game, and the way i’m planning to do it uses the setInterval command. (i think i’m doing it in an odd way, but i’m not experienced with AS so…)

anyway, i was wondering if it is very memory-intensive? because i’ve made a variety of frames which the player does things on, while it pauses (setInterval) before continuing. Anyway, when i run it, it seems to slow down and become uneven in pausetimes (even though i’ve set the same number for them all) before finally just stopping ☹

If so, is there another way to pause, while still allowing movie clips etc to keep going?

also, i need to use the gotoandplay command to a random frame from 1-20, but it says that the ‘random’ command is deprecated, so i should use math.random… problem is, it gives a value b/w 0 and 1, so the only thing i can think of is doing goToAndPlay(random(20))  
any other options?

thanks HEAPS!  
Stuart

---

<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:** [July 20, 2003, 4:58am UTC](https://forum.kirupa.com/t/does-setinterval-lag/26144/2 "2003-07-20T04:58:09Z")

</div>

About the random command… try:[AS]Math.ceil((Math.random())\*20)[/AS]

---

<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:** [July 20, 2003, 8:11am UTC](https://forum.kirupa.com/t/does-setinterval-lag/26144/3 "2003-07-20T08:11:51Z")

</div>

i’m trying to think of other ways to format my game…

if i put the different situations into separate movie clips, could i then make one ‘host’ clip and call the individual ones into it randomly? i guess it would be kinda like loadMovie but with a movieclip instead of a separate swf or jpeg…

and thanks for that help about the math.random thingy 🙂

---

<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:** [July 20, 2003, 10:12am UTC](https://forum.kirupa.com/t/does-setinterval-lag/26144/4 "2003-07-20T10:12:51Z")

</div>

sounds like a job for attachMovie to me…

---

<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:** [July 20, 2003, 9:13pm UTC](https://forum.kirupa.com/t/does-setinterval-lag/26144/5 "2003-07-20T21:13:54Z")

</div>

That random command could be better. There is a slight chance that the outcome will be 0, wich you don’t want. Deprecated means that it can go in the next version of flash, but does not mean it’s wrong to use.

```auto
gototAndPlay(random(20));
// or
gotoAndPlay(Math.round(Math.random() *19) +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:** [July 20, 2003, 10:31pm UTC](https://forum.kirupa.com/t/does-setinterval-lag/26144/6 "2003-07-20T22:31:09Z")

</div>

> \*Originally posted by [m] \*  
> \*\*That random command could be better. There is a slight chance that the outcome will be 0, wich you don’t want. \*\*  
> random(20) returns a random integer between 0 and 20 **including 0**. If you dont want to get a 0, use the following code:[AS]Math.ceil((Math.random())\*20)[/AS]

---

<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:** [July 20, 2003, 11:19pm UTC](https://forum.kirupa.com/t/does-setinterval-lag/26144/7 "2003-07-20T23:19:30Z")

</div>

> _Originally posted by claudio_  
> \*_random(20) returns a random integer between 0 and 20 including 0. If you dont want to get a 0, use the following code:[AS]Math.ceil((Math.random())20)[/AS]_

I guess I should say that I used to think the same thing… senocular proved me wrong. 😛

[http://www.kirupaforum.com/forums/showthread.php?s=&postid=81565#post81565](http://www.kirupaforum.com/forums/showthread.php?s=&postid=81565#post81565)

---

<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:** [July 20, 2003, 11:31pm UTC](https://forum.kirupa.com/t/does-setinterval-lag/26144/8 "2003-07-20T23:31:58Z")

</div>

Hmm now im confused Kax 😛 😛  
Is the code im using wrong? :-\

---

<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:** [July 20, 2003, 11:42pm UTC](https://forum.kirupa.com/t/does-setinterval-lag/26144/9 "2003-07-20T23:42:27Z")

</div>

Yes…  
[AS]Math.ceil(Math.random()\*20);[/AS]  
Returns a number between 0 and 20. 😛

And by the way, [m]…  
[AS]random(20);[/AS]  
Returns a number between 0 and 19. :sigh:

You should use:  
[AS]Math.floor(Math.random()\*20)+1;[/AS]  
😉

---

<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:** [July 20, 2003, 11:52pm UTC](https://forum.kirupa.com/t/does-setinterval-lag/26144/10 "2003-07-20T23:52:08Z")

</div>

Im really confused Kax… :hangover: Ive used the trace command hundreds of times and i got no 0 using[AS]Math.ceil(Math.random()\*20);[/AS]I thought that Math.ceil returns the closest integer that is greater than or equal to the specified number. So i thought that it would never return 0.

:hangover: ☹

---

<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:** [July 21, 2003, 12:03am UTC](https://forum.kirupa.com/t/does-setinterval-lag/26144/11 "2003-07-21T00:03:23Z")

</div>

Yeah, Math.ceil rounds the number to the closest integer that is greater than or equal to the number. That’s correct.

The problem is not the number returned by Math.ceil. The problem is the number returned by Math.random, which could be 0 (unlikely, but still a possibility).

---

<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:** [July 21, 2003, 12:20am UTC](https://forum.kirupa.com/t/does-setinterval-lag/26144/12 "2003-07-21T00:20:13Z")

</div>

Hmm yea i get it. Thanks for the info Kax :thumb:  
I see theres a slight chance that i get 0 using my code. Although I just ran a test using the trace command 7800 times and still got no 0!  
Ahhhh 😛

---

<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:** [July 21, 2003, 12:26am UTC](https://forum.kirupa.com/t/does-setinterval-lag/26144/13 "2003-07-21T00:26:42Z")

</div>

actually, the right way to do it would be:

```auto
Math.floor(Math.random()*19)+1;

```

that would only give you 1-20… same as:

```auto
random(19)+1;

```

but not as much deprecated 🙂

---

<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:** [July 21, 2003, 12:31am UTC](https://forum.kirupa.com/t/does-setinterval-lag/26144/14 "2003-07-21T00:31:04Z")

</div>

Hmm  
Hey Kax, i just ran thousands of tests using:[AS]Math.ceil(Math.random());[/AS]I got “1” all the times.

---

<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:** [July 21, 2003, 12:31am UTC](https://forum.kirupa.com/t/does-setinterval-lag/26144/15 "2003-07-21T00:31:26Z")

</div>

[AS]Math.floor(Math.random()\*19)+1;[/AS]  
Would return a number between 1 and 19. 😛

> _Originally posted by claudio_  
> **Hmm  
> Hey Kax, i just ran thousands of tests using:  
> [AS]Math.ceil(Math.random());[/AS]  
> I got “1” all the times.**

> _Originally posted by kode_  
> The problem is the number returned by Math.random, which could be 0 ( **unlikely, but still a possibility** ).

:sigh:

---

<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:** [July 21, 2003, 12:37am UTC](https://forum.kirupa.com/t/does-setinterval-lag/26144/16 "2003-07-21T00:37:05Z")

</div>

Yea I see.  
Thanks, im just curious about that now.  
I wanna get at least one “0” so i can get some rest 😛

---

<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:** [July 21, 2003, 12:52am UTC](https://forum.kirupa.com/t/does-setinterval-lag/26144/17 "2003-07-21T00:52:02Z")

</div>

you’re right, its 4am, im not totally im my mind. change that 19 to 20, and you have it working right 🙂

---

<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:** [July 21, 2003, 9:43am UTC](https://forum.kirupa.com/t/does-setinterval-lag/26144/18 "2003-07-21T09:43:13Z")

</div>

hmm… i’m totally out of my depth now! 😛

i’m just sticking with the random(20), seems to suit my purposes for now… i’ll have a play around with it sometime

i also stopped using the setInterval command, opting for a tedious time-consuming manual version (inserting heaps of blank frames) because it seemed to keep lagging me…

thanks for all the help anyway guys (-:  
Stuart
