Reputation: 110960
I have the following jQuery:
$.ajax('/contacts_imported', {
dataType : 'json',
data : {
email : cfg.email
},
success : function(data) {
if (data[0].processed) {
alert('Processed is TRUE!');
} else {
alert('Not yet Processed');
}
}
});
What I'm trying to do is, have this ajax request run every second until processed is equal to True. Once True, run a separate functions and kill the timer/loop.
How can I best handle this in an elegant way?
Upvotes: 0
Views: 1204
Reputation: 28400
Do NOT use an interval, as your AJAX request may take longer than 1 second every now and then. So wrap your ajax in a function and call it like this:
(function makeRequest() {
$.ajax('/contacts_imported', {
dataType : 'json',
data : {
email : cfg.email
},
success : function(data) {
if (data[0].processed) {
alert('Processed is TRUE!');
} else {
setTimeout(makeRequest, 800);
}
}
});
}());
Upvotes: 3