Created
May 25, 2012 15:03
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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