1

I searched already the stackoverflow website for this solution, but i can't find any solution that fit with my problem.

I'm requesting a API .. but when that api website is offline it need to be abort/timeout that requests.

Any idea how to do that?

Here is my script:

function getStreamDetails() {
    $.ajaxSetup({ cache: false });
    $.getJSON('https://mywebsite.com/streaminfo.php', function(data) {
        var artist = data['song'];
        var title = data['song'];
        var artistClean = artist.length > 50 ? artist.substring(0, 50) + "..." : artist;

        var music = artistClean;
        var formattedMusic = music.replace(/;/g, ', ');
        var dj = data['live'] == '' ? 'Auto DJ' : data['live'];

        $('#dj').html(dj);
        $('#listeners').html(data['Plays']);
        $('#song').html(formattedMusic);
        $('#song').attr('data-original-title', formattedMusic);
    });
}

setInterval(getStreamDetails, 60000);
getStreamDetails();

Hope someone can help me out :))

Yonas
  • 33
  • 1
  • 9
  • 1
    https://stackoverflow.com/questions/4138470/jquery-getjson-with-timeout – flappix Feb 03 '20 at 21:23
  • 1. Don't use `setInterval()`, use `setTimeout()` instead. To detect a timeout, use the `fail()` promise. – BenM Feb 03 '20 at 21:23
  • @MrD I already tried that, but doens't fit with my code. – Yonas Feb 03 '20 at 21:29
  • @benM What if the url is offline then setTimeout have no effect.. I want to check the url like every X for online, if not then it need to connection timeout the request. – Yonas Feb 03 '20 at 21:29
  • You should call `setTimeout` again once you have a response from the API (regardless of its status). – BenM Feb 03 '20 at 21:32
  • Can you post a snippet about this here? @benm – Yonas Feb 03 '20 at 21:48
  • Possible duplicate of [jQuery getJSON with timeout](https://stackoverflow.com/questions/4138470/jquery-getjson-with-timeout) – MrUpsidown Feb 03 '20 at 23:05

1 Answers1

1

Have a look @ the following article: https://it.toolbox.com/blogs/edmonbegoli/adjusting-jquery-json-timeouts-101508

I will quote from there:

If you are making JSON AJAX calls with jQuery and if you are experiencing errors changing timeout value[...] AJAX call may help alleviate the problem.

If you are working with $.getJSON method, you will notice that there is no option to specify the timeout value. Hence, you have two options: 1. Change the $.getJSON, which is just a wrapper method, to $.ajax. So, from

`$.getJSON("http://someurl", //Gives us parsed JSON automatically 
{ somparam: params }, //The params for the query resultsHandler 
//The callback function that handles JSON results ); 

change the $.getJSON call to $.ajax:

$.ajax({url:"http://someurl", dataType:'json', data: "someparam=" +
params,timeout: 7000, success:resultsHandler }) 

Notice the value for timeout is 7000, or 7 seconds.

For more details please consult $.ajax documentation.

  1. Another alternative is to set the global AJAX parameters before the call to getJSON and then make all your getJSON and AJAX calls with these settings.
$.ajaxSetup({ timeout: 7000 });
$.getJSON("http://someurl", //Gives us parsed JSON automatically {
somparam: params }, //The params for the query resultsHandler //The
callback function that handles JSON results ); 

For more details see $.ajaxSetup documentation.

Menelaos
  • 23,508
  • 18
  • 90
  • 155