diff rlgterm.js @ 73:d084610a89b5

Automatically update the lists of current games and actions. Add some window.*interval() code to getcurrent() so it will register itself as an interval and clear this interval when the list is not visible (i.e. playing or watching.) It also calls getchoices() if the user is logged in, so that will be updated as well.
author John "Elwin" Edwards <elwin@sdf.org>
date Thu, 21 Jun 2012 21:57:04 -0700
parents d7eb63cd7a16
children 2984604ce3e6
line wrap: on
line diff
--- a/rlgterm.js	Thu Jun 21 16:05:59 2012 -0700
+++ b/rlgterm.js	Thu Jun 21 21:57:04 2012 -0700
@@ -85,6 +85,11 @@
   playing: false
 };
 
+/* The interval ID for checking the status of current games. */
+var statInterval = null;
+/* How frequently to check. */
+var statDelta = 8000;
+
 function writeData(hexstr) {
   var codenum;
   var codes = [];
@@ -420,9 +425,17 @@
   return;
 }
 
-function getcurrent() {
-  if (session.id)
+function getcurrent(clear) {
+  if (session.id || clear) {
+    if (statInterval) {
+      window.clearInterval(statInterval);
+      statInterval = null;
+    }
     return;
+  }
+  if (!statInterval) {
+    statInterval = window.setInterval(getcurrent, statDelta);
+  }
   var req = new XMLHttpRequest();
   req.onreadystatechange = function () {
     if (req.readyState != 4 || req.status != 200) 
@@ -477,6 +490,8 @@
   };
   req.open('GET', '/status', true);
   req.send();
+  if (session.lcred)
+    getchoices();
   return;
 }
 
@@ -711,7 +726,6 @@
     document.getElementById("login").style.display = "none";
     document.getElementById("register").style.display = "none";
     document.getElementById("current").style.display = "block";
-    getchoices();
     getcurrent();
   }
   else if (mode == "login") {