Commit 8a12c15f8d4e695711689173e488ff10dfa68c82

Daniel Flint 2014-02-28T06:36:13

Debounce multiple rapid generateCode requests. Changing compression level or on page load causes a large amount of generate code requests as each source file is loaded. Because there's no guarantee the order that these requests will start-and more importantly, end-the generated code can contain undefined code snippets.

diff --git a/download.js b/download.js
index c1006da..f7de2f4 100644
--- a/download.js
+++ b/download.js
@@ -241,9 +241,17 @@ function update(updatedCategory, updatedId){
 		title: prettySize(total.css)
 	});
 	
-	generateCode();
+	delayedGenerateCode();
 }
 
+var timerId = 0;
+// "debounce" multiple rapid requests to generate and highlight code
+function delayedGenerateCode(){
+	if ( timerId !== 0 ) {
+		clearTimeout(timerId);
+	}
+	timerId = setTimeout(generateCode, 500);
+}
 function generateCode(){
 	var code = {js: '', css: ''};