How could I set up a function using jquery that would allow me to flip the text in a certain contain开发者_高级运维er say "#rndbtn" either on success of a .post form or after a certain time like 6000ms.
I am encountering a problem where the request is taking too long so certain browsers will fire off the call again after 60 seconds so I would like it to just flip the text to alert the user it failed after that time rather than tax my server again and again.
$.post("/sendreq", { arg1: arg1, arg2: arg2} ,
function(data,status,xhr) {
if (this.console && typeof console.log != "undefined"){
console.log(status);
console.log(data);
console.log(xhr);
}
if (data == "Error") {
$("#rndbtn").text('Failed');
}
else if (status == "error") {
if (this.console && typeof console.log != "undefined"){
console.log(xhr.status + " " + xhr.statusText + " " +data );
}
$("#rndbtn").text('Failed');
}
else {
if (type!=0) {
if (type!=4) {
$("#rndbtn").text('Sent Successfully');
}
else {
$("#rndbtn").text('Added to List');
}
}
else {
$("#rndbtn").text('Reset Successfully');
}
}
$("#rndbtn").attr("href","");
});
}
$(function() {
//Calling after post
var isSuccess = false;
$.post('/ther/url/here', function(responseData) {
//will get called after the Ajax call was successfull
isSuccess = true;
$('#rndbtn').text('foo');
});
//This will fire 6 seconds after the dom had loaded.
setTimeout(function() {
if(isSuccess === false) {
$('#rndbtn').text('foo');
}
}, 6000);
});
精彩评论