earwig75
asked on
Add timeout to getJSON ajax call
I am using the below ajax to get some JSON from an API. Can someone tell me how I can add a timeout to this function? The fail only works if it fails the call right away.
Thank you.
Thank you.
$.getJSON(foursquareURL).done(function (info) {
var results = info.response.venues[0];
if (typeof self.URL === 'undefined') {
self.URL = '';
}
else {
self.URL = results.url;
}
self.street = results.location.formattedAddress[0] || '';
self.city = results.location.formattedAddress[1] || '';
self.phone = results.contact.phone || '';
}).fail(function () {
$('.list').html('Error.');
});
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Can I display an error when the time out happens?
Can I display an error when the time out happens?Did you see my sample?
The error happens in the fail when the timeout occurs.
ASKER
Thank you!
@earwig75,
I would have split the points between my post and leakim's post. The first post uses setTimeout() which works but is not the best solution in this case when jQuery specifically provides for a timeout.
I would have split the points between my post and leakim's post. The first post uses setTimeout() which works but is not the best solution in this case when jQuery specifically provides for a timeout.
Open in new window