No al cierre de webs
ShareCode
Permalink: http://www.treeweb.es/u/974/ 01/02/2011

ShareCode

1 2 3 var Ajax = function() {4  5  this._xhr = null;6  this.url = '';7  this.method = 'GET';8  this.async = true;9  this.retries = 3;10  this.timeout = 4000; // milliseconds11  this.data = null;12  this.callbacks = ({13  progress:null,14  done:null,15  error:null,16  presend:null17  });18  19 };20 21 Ajax.prototype.send = function(data) {22  this.data = data;23  var that = this;24  25  function go() {26  that._xhr = new XMLHttpRequest();27  that._xhr.timeout = that.timeout;28  that._xhr.open(that.method, that.url, that.async);29  that._xhr.onreadystatechange = function(event) {30  if (that._xhr.readyState == XMLHttpRequest.DONE) {31  if (null != that.callbacks.done) {32  that.callbacks.done(that._xhr);33  }34  }35  };36  37  if (null != that.callbacks.presend)38  that.callbacks.presend(that._xhr);39  that._xhr.send(data);40  };41  42  var timer = function() {43  that.retries--;44  alert(that.retries);45  if (that.retries >0) { // Retry46  if (null !== that._xhr) that._xhr.abort();47  go();48  setTimeout(timer, that.timeout);49  } else { // Lanza error50  alert('error');51  }52  };53  timer();54  55  56  57  /*58  void send();59  void send(ArrayBuffer data);60  void send(Blob data);61  void send(Document data);62  void send(DOMString? data);63  void send(FormData data);64  */65  66  67  68 };69 70 Ajax.prototype.abort = function() {71  this._xhr.abort();72 };73 74 75 76 window.addEventListener('load', function(e){77  var a = new Ajax();78  a.url = 'http://www.treeweb.es/request2';79  a.callbacks.done = function(xhr) {80  alert(xhr.response);81  logger.log(xhr);82  };83  a.send();84  logger.log(a);85 }, true);86 87 88 89 


Este ShareCode tiene versiones:
  1. Ajax Refactor ... (14/12/2012)
  2. Ajax Refactor ... (24/04/2013)
  3. Ajax Refactor ... (24/04/2013)
  4. Ajax Refactor ... (24/04/2013)
  5. Ajax Refactor ... (24/04/2013)
  6. Ajax Refactor ... (24/04/2013)
Enlace
El enlace para compartir es: