Binyamin
Binyamin

Reputation: 7803

jQuery: How to enable `timeout` for `$.ajax({dataType:'jsonp'...`? Is there any solution?

jQuery: How to enable timeout for $.ajax({dataType:'jsonp'...? Is there any solution? http://jsfiddle.net/laukstein/2wcpU/4

$.ajax({
    type:"GET",
    url:'http://lab.laukstein.com/ajax-seo/.json',
    dataType:'jsonp',
    timeout:200, // Not working with dataType:'jsonp'
    success:function(data){$('#content').html(data.content);},
    error:function(request,status,error){$('#content').html('request failed');}
});

I do not like to use some plugins for that, like http://code.google.com/p/jquery-jsonp.

Upvotes: 6

Views: 4523

Answers (2)

Binyamin
Binyamin

Reputation: 7803

Here is my solution with setTimeout and clearTimeout http://jsfiddle.net/laukstein/2wcpU/7/

$('#content').ajaxStart(function(){
    $(this).html('Loading...');
});
var timer=window.setTimeout(function(){
    $('#content').html('Loading seems to be taking a while. Try again.');
},2000);
$.ajax({
    type:"GET",
    url:'http://lab.laukstein.com/ajax-seo/.json',
    dataType:'jsonp',
    success:function(data){
        window.clearTimeout(timer);
        $('#content').html(data.content);
    },
    error:function(){
        window.clearTimeout(timer);
        $('#content').html('The request failed. Try to refresh page.');
    }
});

Upvotes: 4

jim tollan
jim tollan

Reputation: 22485

Binyamin,

this SO answer should help you:

jQuery ajax (jsonp) ignores a timeout and doesn't fire the error event

basically, suggests using jquery.jsonp instead of $ajax

Upvotes: 0

Related Questions