/** @const @type {number} */ var POLL_INTERVAL = 5000; function doGet(url, callback) { var xhr = new XMLHttpRequest(); xhr.onreadystatechange = function(e) { if (xhr.readyState === 4) { var resp = null; if (xhr.status === 200) { resp = xhr.response; try { resp = JSON.parse(/** @type {string} */ (resp)); } catch (e) { resp = null; } } callback(xhr.status, resp); } }; xhr.open('GET', url, true); xhr.send(null); } function initPolling() { lazyGetPseudonyme(function(pseudo) { if (pseudo) { doGet("/api/poll?grid=" +GRID_PUBLIC_ID +"&v=" +KNOWN_VERSION, function(status, resp) { if (resp) { GRID = new Grid(resp); updateOnPollResult(resp); GRID.playerSelf = GRID.players[pseudo]; scheduleNextPoll(); } // TODO else cannot init party }); } // TODO else cannot init pseudo }); } function pollNow() { if (pollNow.polling !== true) { if (pollNow.pollSchedule) { clearTimeout(pollNow.pollSchedule); pollNow.pollSchedule = 0; } pollNow.polling = true; doGet("/api/poll?grid=" +GRID_PUBLIC_ID +"&v=" +KNOWN_VERSION, function(status, resp) { pollNow.polling = false; if (resp) { updateOnPollResult(resp); } scheduleNextPoll(); }); } } function scheduleNextPoll() { if (!pollNow.pollSchedule) pollNow.pollSchedule = setInterval(pollNow, POLL_INTERVAL); };