How To Run Ajax Call In Background After Some Particulate Time?
I want to show alert message after some perticulate time as 'Do you want continue?' If user say yes then ajax call must be run in background otherwise cancel the ajax call. So ple
Solution 1:
In technical terms it called as Long Polling request.. its something like this
functionlongPolling()
{
$.ajax({
//..... your ajax configurations like url, type, dataType...etcsuccess: function(data){
// your code for handle success responsesetTimeout(function(){
longPolling();
},5000) // call again after 5 seconds
},
error: function(xhr){
// your code for handle error responsesetTimeout(function(){
longPolling();
},5000) // call again after 5 seconds
}
};
});
longPolling();// call where ever you need
I hope it help to you
Solution 2:
Solution 3:
var time = 5000;
setTimeout(function() {
if (confirm('Do you want continue?')) {
var root = 'http://jsonplaceholder.typicode.com';
$.ajax({
url: root + '/posts/1',
method: 'GET'
}).then(function(data) {
console.log(data);
});
} else {
// Do nothing!
}
}, time)
<scriptsrc="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
Solution 4:
What i understood from your question is that the ajax call will be made before you ask the user "whether you would like to continue or not" correct?
And i believe you wanted to make sure the user want to wait if the ajax call is taking long time?
if this is the case you just set a flag on user press "no" and just abort the ajax call
// Assign handlers immediately after making the request,// and remember the jqXHR object for this requestvar jqxhr = $.ajax( "example.php" )
.done(function() {
alert( "success" );
})
.fail(function() {
alert( "error" );
})
});
//when user pressed no call the below function if the ajax is not completed
jqxhr.abort()
Solution 5:
window.setTimeout(function () {
if (confirm('Do you want continue?')) {
// Do some ajax?
} else {
// Do nothing!
}
}, 5000);
Post a Comment for "How To Run Ajax Call In Background After Some Particulate Time?"