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.

  $.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.');
  });

Open in new window

earwig75Asked:
Who is Participating?
 
leakim971Connect With a Mentor PluritechnicianCommented:
if you want to use the timeout for all you call, you may use $.ajaxSetup({ timeout: 2000 });
it gonna be available for all your ajax call after this setup.

  $.ajaxSetup({ timeout: 2000 });
  $.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.');
  });

Open in new window

0
 
Swatantra BhargavaTechnology no BarCommented:
Try Following

var retVal = $.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.');
  });

setTimeout(function(){ retVal.abort(); }, 2000);

Open in new window

0
 
Julian HansenConnect With a Mentor Commented:
This is provided for in jQuery's AJAX function. getJSON() is an alias of the AJAX function.

To use the ajax() function to get a JSON response with a timeout you do like this

$.ajax({
  url: foursquareURL,
  type: 'GET',
  dataType: 'JSON',
  timeout: 2000
}).then(function(resp) {
  // Success code here
})
.fail(xhr, status, error) {
  // handle error / timeout here
});

Open in new window


Working sample here
0
Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

 
earwig75Author Commented:
Can I display an error when the time out happens?
0
 
Julian HansenCommented:
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.
0
 
earwig75Author Commented:
Thank you!
0
 
Julian HansenCommented:
@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.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.