如何声明“beforeClose”使用 CEP/JavaScript 一次事件?
我需要知道用户何时决定关闭他所在的文档,因此我在各个站点上找到了“beforeClose”事件。
我有两个问题:
- 当我在 ExtendScript 文件中声明它时,第一次,该事件被称为 2x(单击时为 1x,文档完全关闭时为 1x),这正常吗?
- 当我关闭扩展并重新打开它时,事件会再次声明 并被称为 1x 更多(4x 而不是 2x),依此类推,每次我关闭 并重新打开扩展程序。我怎样才能只声明一次事件?喜欢 在 JavaScript 端声明的事件,例如 “激活后的文档”?
以下是我目前在 jsx 文件中执行此操作的方法:
main();
function main() {
app.addEventListener("beforeClose", detectClose);
}
function detectClose() {
alert('The document is closed');
}
提前感谢您的帮助!
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 !
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不确定是否理解它应该如何工作,但我看到两个选择。 (1) 您可以检查是否已经有相同事件类型的侦听器,只有在没有此类侦听器时才添加新侦听器:
(2) 或者您可以使用如下函数预先删除所有侦听器(对于给定事件类型) :
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:
(2) Or you can remove all listeners (for given event type) beforehand with a function like this: