<p>What is the most recommended/best way to stop multiple instances of a setTimeout function from being created (in javascript)?</p>

<p>An example (psuedo code):</p>

<pre><code>function mouseClick()
{
   moveDiv("div_0001", mouseX, mouseY);
}

function moveDiv(objID, destX, destY)
{
   //some code that moves the div closer to destination
   ...
   ...
   ...

   setTimeout("moveDiv(objID, destX, destY)", 1000);
   ...
   ...
   ...
}
</code></pre>

<p>My issue is that if the user clicks the mouse multiple times, I have multiple instances of moveDiv() getting called.</p>

<p>The option I have seen is to create a flag, that only allows the timeout to be called if no other instance is available...is that the best way to go?</p>

<p>I hope that makes it clear....</p>

```
```
var timeout1 = window.setTimeout('doSomething();', 1000);
var timeout2 = window.setTimeout('doSomething();', 1000);
var timeout3 = window.setTimeout('doSomething();', 1000);

// to cancel:
window.clearTimeout(timeout1);
window.clearTimeout(timeout2);
window.clearTimeout(timeout3);
```
```