Dynamic updates. The Javascript code estimates the time taken to
[cocanwiki.git] / html / _js / editor.js
index 84b930e..d4dc063 100644 (file)
@@ -1,9 +1,17 @@
 /* Javascript for OCAMLWIKI.
  * Copyright (C) 2004 Merjis Ltd.
- * $Id: editor.js,v 1.4 2004/10/26 17:50:23 rich Exp $
+ * $Id: editor.js,v 1.5 2004/11/14 14:23:27 rich Exp $
  */
 
-var delay = 1000;              // Delay in milliseconds before updating.
+// Delay in milliseconds before updating.
+// We will adjust this during editing to take into account the actual
+// average round trip time measured during update_preview_now.
+var delay = 1000;
+
+// This is used to measure the average round trip time.
+var rtt_sum = 0;
+var rtt_n = 0;
+var rtt = 0;                   // Actual average RTT measured (ms).
 
 function update_preview (content_id, preview_id)
 {
@@ -25,6 +33,9 @@ function update_preview_now (content_id, preview_id)
   // Get the Wiki-markup content from the content textarea.
   var content = document.getElementById (content_id).value;
 
+  // Time how long it takes to send the document to the server.
+  var start_time = (new Date()).getTime();
+
   // Send the Wiki-markup to the server to be turned into XHTML.
   var http = document.all ?
     new ActiveXObject ('Microsoft.XMLHTTP') : new XMLHttpRequest ();
@@ -42,6 +53,15 @@ function update_preview_now (content_id, preview_id)
       // "Unknown runtime error"
       preview.innerHTML = xhtml;
     }
+
+  // Finish timer and recompute RTT.
+  var end_time = (new Date()).getTime();
+  rtt_sum += end_time - start_time;
+  rtt_n++;
+  rtt = rtt_sum / rtt_n;
+
+  // Recompute the next delay period.
+  delay = rtt * 2;
 }
 
 // Initialise the edit_buttons for a section.