Prevent duplicate drupal_add_js calls
Safest and most straightforward way is to move js file to module's info file, and include it always. just make sure it works only on elements it should, and on all of them. For this you need appropriate classes.

Other way is to use ['#attached']['js'] of highest possible element, instead of calling drupal_add_js manually. if you will set script twice, you will override older instance, so no problem.

If, for reasons unknown to me, you decide you have to call drupal_add_js yourself, use drupal_static to make sure you didn't call it already. It will require least changes in existing code - and in my opinion is worst solution. But will work. Typical usage is:

$mycounter = &amp;drupal_static(__FUNCTION__, 0);


That makes $mycounter shared among all scopes where it is called, and initialized to 0. Then, you can test if it's still 0, and if it is - add your JS and increment counter with good old $mycounter++