I use $.ajax()
method from JQuery
to do some POST requests, and inside its error: function(jqXHR, textStatus, errorThrown) { ... }
callback, I want to implement a simple retry mechanism with timeout, eg
timeout = window.setTimeout(function() {
$.ajax(myReqOptions);
}, 3000);
myReqOptions
here needs to be the options object I define in $.ajax()
to make this work. Is there a convenient way to reference this object other than setting a temp variable before calling the timeout, like this:
myReqOptions = this;
timeout = window.setTimeout(function() {
$.ajax(myReqOptions);
}, 3000);
this
here refers to the owner of the error
callback, which is the object I define in $.ajax()
.
The temp variable is fine. To shorten the call a littlebit, you can use the bind
function method :
timeout = window.setTimeout($.ajax.bind($, this), 3000);
or, for compatibility with old browser (not using the ES5 shim), with $.proxy
:
timeout = window.setTimeout($, "ajax", this), 3000);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.