浏览代码

limit how often the notes window updates presentation states

Hakim El Hattab 11 年之前
父节点
当前提交
f396b9b871
共有 1 个文件被更改,包括 34 次插入0 次删除
  1. 34 0
      plugin/notes/notes.html

+ 34 - 0
plugin/notes/notes.html

@@ -244,6 +244,9 @@
 
 				}
 
+				// Limit to max one state update per X ms
+				handleStateMessage = debounce( handleStateMessage, 200 );
+
 				/**
 				 * 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>