I came across the following script quite often, so I'm wondering if there is a built-in jQuery way to solve the problem.
Provide the following code:
$(document).click(function() { paintCanvas(); });
The problem with this code is that if the user clicks on the screen 50 times in a row, you overload the browser with 50 calls of paintCanvas.
If paintCanvas is currently running and a new request is being created, we want to queue a new request so that it waits for paintCanvas to complete. However, at the same time, we can drop any calls that were in the queue for paintCanvas, since we care only about the final state of the mouse, and not about all intermediate states.
Here is the code that solves the problem:
var _isExecuting, _isQueued; function paintCanvas() { if (_isExecuting) { if (!_isQueued) { _isQueued = true; setTimeout(function() { _isQueued = false; paintCanvas(); }, 150); } return; } _isExecuting = true;
This AJAX queue plugin essentially implements this functionality, but does so only from an AJAX perspective. Of course, this is a very common problem that can be solved in a more general way?
source share