How to do sequential asynchronous ajax requests with given number of streams

前端 未结 3 1317
忘掉有多难
忘掉有多难 2020-12-31 21:21

I need to make sequential asynchronous ajax requests with limited streams. As of now I am allowed to occupy only one stream on web server so I can only do one ajax request a

3条回答
  •  别那么骄傲
    2020-12-31 21:25

    As long as your callbacks are all synchronous this should work for you, if not put you on the right track

    var initiateChain = function () {
    
        var args = arguments,
            index = 0,
            length = args.length,
            process = function ( index ) {
    
                if ( index < length ) {
                    $.ajax({
                        url: '/example.php',
                        complete: function () {
                            // Callbacks get run here
                            args[ index ];
                            process( ++index );
                        }
    
                    });
                }
    
    
            };
    
        if ( length ) {
            process( 0 );
        }
    
    };
    
    initiateChain( getGadgets, getDeals, getDeals );
    

提交回复
热议问题