Is it possible to throttle documentChanged events?
I'm implementing a "live preview" function in my plugin. I added the document listener to my editor, but it fires every time the user types anything, and this creates a lot of noise. I'm wondering if there's any built-in mechanism which would allow me to throttle those events, e.g. limiting to 1 per second or so.
Please sign in to leave a comment.
Eugene, you can implement it using API
Alarmclass:Thanks, Vladimir, that looks good, but how do I add it to my document or document listener?
Add the members of the
ThrottleDocumentChangedto your class that is responsible for updating the preview, if you have a custom editor for the document add it there, and from theDocumentListenercall theupdateDocument()method to trigger the next update.The delay in the sample is 50ms, you can make it what you feel gives the best compromise between frequent updates and short enough update delay.
An update will occur 50ms after the last call to
updateDocument().Got it, thank you very much! This is precisely what I was looking for!