最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to set timeout on $.ajax request and redo it if it takes too long? - Stack Overflow

programmeradmin2浏览0评论

Can someone show me a practical example on setting a timeout to my $.ajax request and redo the entire request if the first request is timed out, I've read the docs and didn't get it. I will appreciate any help.

Here is my $.ajax request.

    $.ajax({
        url: '<?php bloginfo('template_directory'); ?>/ajax/product.php',
        type: 'get',
        data: {product_id : product_id},
        beforeSend: function(){
            $('#details').html('<div class="loading"></div>');
        },
        success: function(data){
            $('.iosSlider').fadeOut('fast');
            thisprod.addClass('current');
            $('#details').css({opacity: 0}).html(data).stop().animate({left: 0, opacity: 1}, 800);
        }
    });
    return false;

Can someone show me a practical example on setting a timeout to my $.ajax request and redo the entire request if the first request is timed out, I've read the docs and didn't get it. I will appreciate any help.

Here is my $.ajax request.

    $.ajax({
        url: '<?php bloginfo('template_directory'); ?>/ajax/product.php',
        type: 'get',
        data: {product_id : product_id},
        beforeSend: function(){
            $('#details').html('<div class="loading"></div>');
        },
        success: function(data){
            $('.iosSlider').fadeOut('fast');
            thisprod.addClass('current');
            $('#details').css({opacity: 0}).html(data).stop().animate({left: 0, opacity: 1}, 800);
        }
    });
    return false;
Share Improve this question edited Sep 4, 2012 at 15:06 Denys Séguret 383k90 gold badges811 silver badges776 bronze badges asked Sep 4, 2012 at 14:59 Ahmed FouadAhmed Fouad 3,07310 gold badges31 silver badges54 bronze badges 1
  • possible duplicate of How do I resend a failed ajax request? – user2284570 Commented Oct 10, 2014 at 19:27
Add a ment  | 

4 Answers 4

Reset to default 7

The ajax function takes a timeout parameter and you can check the status in case of error.

var call =function(){
    $.ajax({
        url: '<?php bloginfo('template_directory'); ?>/ajax/product.php',
        type: 'get',
        timeout: 400,
        ...
        error: function(x, textStatus, m) {
            if (textStatus=="timeout") {
                 call();
            }
        }
    });
};

You might want to make something a little smarter to avoid permanent calls...

From the documentation :

Set a timeout (in milliseconds) for the request. This will override any global timeout set with $.ajaxSetup(). The timeout period starts at the point the $.ajax call is made; if several other requests are in progress and the browser has no connections available, it is possible for a request to time out before it can be sent. In jQuery 1.4.x and below, the XMLHttpRequest object will be in an invalid state if the request times out; accessing any object members may throw an exception. In Firefox 3.0+ only, script and JSONP requests cannot be cancelled by a timeout; the script will run even if it arrives after the timeout period.

I would use the jquery deferred fail callback for this case. http://api.jquery./deferred.fail/

a. You can maintain a queue of all the requests that you make.

$.xhrQueue = [];

b. Enqueue each request that you make

$.ajaxSetup({
    beforeSend: function (e, xhr) {  
            $.xhrQueue .push(xhr);
        }
});

c. Poll for which requests pleted vs timed-out

setInterval(function(){
    $.each($.xhrQueue, function (xhr) {
       //check whether status is plete, 
       //with some try-catch you can know which were the timed-out/not-sent ones

    });
}, 1000);

Note: If not beforeSend, you can go via some other function through which you attempt to make an ajax call

Try this

var setTime = setTimeOut(function() {
    $.ajax({
        url: '<?php bloginfo('
        template_directory ');?>/ajax/product.php',
        type: 'GET',
        data: {
            'product_id': product_id
        }
    }).beforeSend(function() {
        $('#details').html('<div class="loading"></div>');
    }).done(function(data) {
        $('.iosSlider').fadeOut('fast');
        thisprod.addClass('current');
        $('#details').css({
            opacity: 0
        }).html(data).stop().animate({
            left: 0,
            opacity: 1
        }, 800);
    });
}, 2000);
发布评论

评论列表(0)

  1. 暂无评论