limit how often the notes window updates presentation states

This commit is contained in:
Hakim El Hattab 2014-04-22 14:50:50 +02:00
parent c02d185cfd
commit f396b9b871
1 changed files with 34 additions and 0 deletions

View File

@ -244,6 +244,9 @@
} }
// Limit to max one state update per X ms
handleStateMessage = debounce( handleStateMessage, 200 );
/** /**
* Creates the preview iframes. * Creates the preview iframes.
*/ */
@ -340,6 +343,37 @@
} }
/**
* Limits the frequency at which a function can be called.
*/
function debounce( fn, ms ) {
var lastTime = 0,
timeout;
return function() {
var args = arguments;
var context = this;
clearTimeout( timeout );
var timeSinceLastCall = Date.now() - lastTime;
if( timeSinceLastCall > ms ) {
fn.apply( context, args );
lastTime = Date.now();
}
else {
timeout = setTimeout( function() {
fn.apply( context, args );
lastTime = Date.now();
}, ms - timeSinceLastCall );
}
}
}
})(); })();
</script> </script>