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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Swatantra BhargavaTechnical Specialist/DeveloperCommented:
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 HansenCommented:
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
leakim971PluritechnicianCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Become a Microsoft Certified Solutions Expert

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
jQuery

From novice to tech pro — start learning today.