I was working on a dropdown widget where the user could scroll down using the arrow keys, and while he was scrolling, two things needed to happen:
- UI need to be updated, showing his new selection, and removing his old selection.
- Some costly DOM operations had to happen in the background to set the selected item in the widget.
I also knew that i only really needed to do the DOM operations for the last item selected, that is the one the user really wanted.
What i needed to do was to split up the scroll function into two components, first call the UI update every time the arrow was pressed, and second, queue up the expensive DOM operations to happen in the future, but dont execute them until no new DOM operations have been queued for some interval of time. This would give the user the visual feedback of "something is happening" while not executing the costly operation that would make everything feel slow and unresponsive.
The plugin i came up with lets you specify the interval that you will wait for further requests until you execute the last request, and the number of times you will wait that interval.
One thing to be aware of is that this plugin does not implement any queing internally, so if you should only use one instance of it per function. make a new instance if you want to improve the performance of a different function.
For more info such as:
- A simple typing demo
- A crappy implementation of hoverIntent plugin