Quantcast
Channel: Adobe Community : Popular Discussions - InDesign Scripting
Viewing all articles
Browse latest Browse all 15932

[CS6/JS] Best practices checking whether eventListener already exists or preventing duplicate calls?

$
0
0

Running the following piece of code in a script will register a javascript function named "afterSelectionChanged" to be run as soon as the selection is changed – the second parameter is the function name:

app.eventListeners.add("afterSelectionChanged", afterSelectionChanged);

 

My problem concerns unregistering this event listener, or rather preventing it from being registered again. I'm using a specified "targetengine", so running the script again and again will add a new function to be called every time the script is started, resulting in the same function being called multiple times.

 

In the main function (startup) of my script, I could loop through the app.eventListeners collection, and remove any eventListeners whose .handler == 'afterSelectionChanged()' or eventType == 'afterSelectionChanged' like this:

 

for (var elIndex = app.eventListeners.length-1; elIndex >=0; elIndex--){    if (app.eventListeners[elIndex].eventType == 'afterSelectionChanged'){        app.eventListeners[elIndex].remove();    }
}

Is this a good practice?

Or is it unnecessary to check for specific event names at all, that is: should I rather remove all eventListeners, or could that disturb something? I have a #targetengine directive with a specified name, at the top of my script.

 

Are you supposed to try to remove all eventhandlers and / or kill your targeted engine when a ScriptUI window is closed?

 

Thanks,

Andreas


Viewing all articles
Browse latest Browse all 15932

Trending Articles


FLASHBACK WITH SIRASA FM AT GALGAMUWA 2022


Mp3 Download: Mdu - Mazola


Imitation gun was fired at motorist in Leicester road-rage incident


Ndebele names


MCKINNEY EMALINE “EMMA” OF WES...


Okra & Motia — The Workshop (Prod by Hammer)


Skint TV teen to be sentenced


Moondru Mudichu 19-09-2017 – Polimer tv Serial


YOSVANI JAMES Arrested by Miami-Dade County Corrections on Jan 10, 2017


Stories • Goddess Stepmom