Skip to content

Instantly share code, notes, and snippets.

@miohtama
Created May 25, 2012 15:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save miohtama/2788628 to your computer and use it in GitHub Desktop.
Save miohtama/2788628 to your computer and use it in GitHub Desktop.
Throttle Javascript events so that the same event doesn't fire twice in the timeframe
/**
* Kindly ask to redraw the one frame preview.
*
* This function has throttling support so you can
* fire this from mouse move events (color picking) safely.
*
* XXX: Make sure all the fonts are loaded in this point or the
* first round of text rendering fails. See hidden font preloader in creator.pt.
*
* @param elem Element to redraw. null to clear, undefined ot use selected.
*/
requestOneFramePreview : function(elem) {
var self = this;
if(elem === undefined) {
elem = this.organizer.selected;
}
function redraw() {
self.oneFramePreviewTimeout = null;
if(elem) {
var data = elem.data("input-element");
console.log(data);
} else {
console.log("Flushing preview");
}
self.updateOneFramePreview(elem);
}
if(this.oneFramePreviewTimeout) {
window.clearTimeout(this.oneFramePreviewTimeout);
}
this.oneFramePreviewTimeout = window.setTimeout(redraw, 450);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment