Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

317
Visualizações
How to declare the "beforeClose" event once time using CEP/JavaScript?

I need to know when the user decides to close the document he is on, so I found on various sites the "beforeClose" event.

I have 2 problems:

  • When I declare it in my ExtendScript file, the 1st time, the event is called 2x (1x when clicking and 1x when the doc is completely closed) is it normal ?
  • When I close the extension and reopen it, the event is declared again and is called 1x more (4x instead of 2x) and so on each time I close and reopen the extension. How can I declare the event only once ? Like the events that are declared on the JavaScript side like "documentAfterActivate" ?

Here is how I currently do it in my jsx file :

main();

function main() {
    app.addEventListener("beforeClose", detectClose);
}

function detectClose() {
    alert('The document is closed'); 
}

Thank you in advance for your help !

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Not sure if understand how it's supposed to work, but I see two options. (1) You can check if there already are listener for the same event type and add the new listener only if there are no such listeners:

main();

function main() {
    if (if_not_exists('beforeClose')) app.addEventListener('beforeClose', detectClose);
}

function detectClose() {
    alert('The document is closed');
}

function if_not_exists(eventType) {
    var listeners = app.eventListeners;
    var i = listeners.length
    while (i--) if (listeners[i].eventType == eventType) return false;
    return true;
}

(2) Or you can remove all listeners (for given event type) beforehand with a function like this:

function remove_listeners(eventType) {
    var listeners = app.eventListeners;
    var i = listeners.length
    while (i--) if (listeners[i].eventType == eventType) listeners[i].remove();
}
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda