comparison 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
comparison
equal deleted inserted replaced
72:95bf956bce5a 73:d084610a89b5
83 lcred: null, 83 lcred: null,
84 /* Whether the game is being played or just watched. */ 84 /* Whether the game is being played or just watched. */
85 playing: false 85 playing: false
86 }; 86 };
87 87
88 /* The interval ID for checking the status of current games. */
89 var statInterval = null;
90 /* How frequently to check. */
91 var statDelta = 8000;
92
88 function writeData(hexstr) { 93 function writeData(hexstr) {
89 var codenum; 94 var codenum;
90 var codes = []; 95 var codes = [];
91 var nc; 96 var nc;
92 var u8wait = 0; /* Stores bits from previous bytes of multibyte sequences. */ 97 var u8wait = 0; /* Stores bits from previous bytes of multibyte sequences. */
418 req.open('POST', '/login', true); 423 req.open('POST', '/login', true);
419 req.send(JSON.stringify(loginmsg)); 424 req.send(JSON.stringify(loginmsg));
420 return; 425 return;
421 } 426 }
422 427
423 function getcurrent() { 428 function getcurrent(clear) {
424 if (session.id) 429 if (session.id || clear) {
425 return; 430 if (statInterval) {
431 window.clearInterval(statInterval);
432 statInterval = null;
433 }
434 return;
435 }
436 if (!statInterval) {
437 statInterval = window.setInterval(getcurrent, statDelta);
438 }
426 var req = new XMLHttpRequest(); 439 var req = new XMLHttpRequest();
427 req.onreadystatechange = function () { 440 req.onreadystatechange = function () {
428 if (req.readyState != 4 || req.status != 200) 441 if (req.readyState != 4 || req.status != 200)
429 return; 442 return;
430 var reply; 443 var reply;
475 gamediv.appendChild(row); 488 gamediv.appendChild(row);
476 } 489 }
477 }; 490 };
478 req.open('GET', '/status', true); 491 req.open('GET', '/status', true);
479 req.send(); 492 req.send();
493 if (session.lcred)
494 getchoices();
480 return; 495 return;
481 } 496 }
482 497
483 function getchoices() { 498 function getchoices() {
484 if (session.id != null || !session.lcred) 499 if (session.id != null || !session.lcred)
709 document.getElementById("keyboard").style.display = "none"; 724 document.getElementById("keyboard").style.display = "none";
710 document.getElementById("startgame").style.display = "block"; 725 document.getElementById("startgame").style.display = "block";
711 document.getElementById("login").style.display = "none"; 726 document.getElementById("login").style.display = "none";
712 document.getElementById("register").style.display = "none"; 727 document.getElementById("register").style.display = "none";
713 document.getElementById("current").style.display = "block"; 728 document.getElementById("current").style.display = "block";
714 getchoices();
715 getcurrent(); 729 getcurrent();
716 } 730 }
717 else if (mode == "login") { 731 else if (mode == "login") {
718 document.getElementById("keyboard").style.display = "none"; 732 document.getElementById("keyboard").style.display = "none";
719 document.getElementById("startgame").style.display = "none"; 733 document.getElementById("startgame").style.display = "none";