jQuery getJSON with timeout

Scott B picture Scott B · Nov 9, 2010 · Viewed 30.4k times · Source

When making a call out to the yahoo web service (http://boss.yahooapis.com/ysearch) to return a data set, is it possible to set a timeout and exit the routine once its elapsed?

jQuery.getJSON("http://boss.yahooapis.com/ysearch/...etc",
        function (data) {
              //result set here
            });

Answer

Galen picture Galen · Nov 9, 2010

You can use the timeout option

http://api.jquery.com/jQuery.ajax/

$.ajax({
  url: url,
  dataType: 'json',
  data: data,
  success: callback,
  timeout: 3000 //3 second timeout
});