function load() { var base = '//127.0.0.1:9842', ws = new WebSocket('ws:' + base + '/ws'); ws.onopen = function(event) { document.location.href = 'http:' + base; }; ws.onerror = function(event) { ws.close(); setTimeout(load, 500); }; ws.onclose = function(event) { setTimeout(load, 500); }; }; function update() { var xhr = new XMLHttpRequest(); xhr.onload = function() { var response = JSON.parse(this.responseText); if (response.step) { var status = response.step; if (response.progress) { status = parseInt(response.progress * 100) + '% ' + status; } document.getElementById('status').innerHTML = status; setTimeout(update, 1000); } else { document.getElementById('status').innerHTML = "done"; setTimeout(load, 500); } }; xhr.onerror = function() { var status = document.getElementById('status').innerHTML; if (['done', 'setup'].indexOf(status) == -1) { document.getElementById('status').innerHTML = "error"; } load(); } xhr.open('get', '/status'); xhr.send(); } update();