如何在特定时间间隔内忽略VSCODE事件?
例如,如果我触发 ondidchangeTextDocument
连续事件,最大间隔为1秒钟,那么我想避免使用此事件的关联逻辑。
但是,如果触发了最后的 ondidchangeTextDocument
以来1秒钟已经过去了,我希望它继续其逻辑。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这种方法被称为“合并事件”。一种典型的方法是带有所需的间隔。每个新事件都会重新启动计时器,因此除非计时器值中没有新事件,否则它永远不会触发。然后,触发的计时器调用任何进一步的代码,处理合并事件。如果您必须处理每个事件发送的所有数据,这可能会导致一些问题。在这种情况下,您必须收集有关活动处理程序的每个调用的数据。
这是在VS代码扩展程序中处理每个文件的代码:
This approach is known as "coalescing events". A typical approach is to start a timer when an event appears with the required interval. Every new event restarts the timer, so it never triggers unless there are no new events within the timer value. The triggered timer then calls any further code, handling the coalesced events. This might cause some problems if you have to process all data sent by each event. In that case you have to collect that data on each invocation of your event handler.
Here's code to handle changes per file in a VS Code extension: