# Trying to figure out functions

**URL:** <https://forum.kirupa.com/t/trying-to-figure-out-functions/266028>\
**Category:** flash\
**Created:** [July 15, 2008, 4:35pm UTC](https://forum.kirupa.com/t/trying-to-figure-out-functions/266028 "2008-07-15T16:35:14Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![goatomatic](https://avatars.discourse-cdn.com/v4/letter/g/c0e974/32.png) [@goatomatic](https://forum.kirupa.com/u/goatomatic)\
**Post date:** [July 15, 2008, 4:35pm UTC](https://forum.kirupa.com/t/trying-to-figure-out-functions/266028/1 "2008-07-15T16:35:14Z")

</div>

Hey, I’m kind of a beginner here so bear with me. I’m trying to make camera flashes in a crowd with random locations and a random time between flashes. I made a function that works, but I’d really like to make it more reusable, so that I could have two flasher functions running at once. I’m having a lot of trouble figuring out how to reference just the instance created in the function, anyway here’s the code.

```auto

randomRange = function(min, max){
    return Math.floor(Math.random()*(max-min+1)) + min;
}

function flasher(){
    clearInterval(intName);
    _root.attachMovie("flash", "flash1", 10);
    rdmNum = randomRange(500, 3000);
    flash1._x = randomRange(10, 540);
    flash1._y = randomRange(10, 390);
    intName = setInterval(flasher, rdmNum); 
    //trace("1 = "+rdmNum);
}

flasher();

```

Any tips?

---

<div class="post-metadata">

**Author:** ![goatomatic](https://avatars.discourse-cdn.com/v4/letter/g/c0e974/32.png) [@goatomatic](https://forum.kirupa.com/u/goatomatic)\
**Post date:** [July 15, 2008, 7:12pm UTC](https://forum.kirupa.com/t/trying-to-figure-out-functions/266028/2 "2008-07-15T19:12:05Z")

</div>

Ok, I figured out one part. I just need a way to give the interval a unique name so that each time it is calledit doesn’t interfere with the rest. Every time I try to make it unique it seems to break the clearInterval function. Ideas? Here’s what I have so far

```auto

randomRange = function (min, max) {
    return Math.floor(Math.random()*(max-min+1))+min;
};

function flasher(num) {
    clearInterval(interval);
    var t = _root.attachMovie("flash", "flash"+num, num+1);
    rdmNum = randomRange(500, 3000);
    t._x = randomRange(10, 540);
    t._y = randomRange(10, 390);
    interval = setInterval(flasher, rdmNum, num);
}

flasher(1);
flasher(2);

```

---

<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, 9:57pm UTC](https://forum.kirupa.com/t/trying-to-figure-out-functions/266028/3 "2008-07-15T21:57:28Z")

</div>

It’s probably easier if you don’t have to keep track of intervals at all:

```auto
function flasher(num) {
    var t = _root.attachMovie("flash", "flash"+num, num+1);
    rdmNum = randomRange(500, 3000);
    t._x = randomRange(10, 540);
    t._y = randomRange(10, 390);
    setTimeout(flasher, rdmNum, num);
}

```
