Browse Source

more robust maybe

master
Martin Dørum 4 years ago
parent
commit
35bb93cd58
2 changed files with 11 additions and 1 deletions
  1. 8
    1
      client.js
  2. 3
    0
      test.js

+ 8
- 1
client.js View File

@@ -2,7 +2,13 @@
function post(url, cb) {
var xhr = new XMLHttpRequest();

let timeout = setTimeout(function() {
console.log("Timed out, aborting.");
xhr.abort();
}, 3000);

xhr.addEventListener("load", function() {
clearTimeout(timeout);
try {
var obj = JSON.parse(xhr.responseText);
} catch (err) {
@@ -18,10 +24,11 @@
});

xhr.addEventListener("error", function(err) {
clearTimeout(timeout);
cb(err);
});
xhr.addEventListener("abort", function(err) {
console.log("Aborted.");
clearTimeout(timeout);
cb(err);
});


+ 3
- 0
test.js View File

@@ -28,6 +28,9 @@ var eventer = Eventer();

http.createServer(function(req, res) {
if (req.url == "/") {
res.writeHead(200, {
"Content-Type": "text/html",
});
res.end(html);
} else if (req.url == "/client.js") {
fs.createReadStream("client.js")

Loading…
Cancel
Save