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

function post(url, cb) { function post(url, cb) {
var xhr = new XMLHttpRequest(); var xhr = new XMLHttpRequest();


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

xhr.addEventListener("load", function() { xhr.addEventListener("load", function() {
clearTimeout(timeout);
try { try {
var obj = JSON.parse(xhr.responseText); var obj = JSON.parse(xhr.responseText);
} catch (err) { } catch (err) {
}); });


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



+ 3
- 0
test.js View File



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

Loading…
Cancel
Save