jQuery ajax loop and iteration scope

前端 未结 4 735
终归单人心
终归单人心 2021-01-25 23:07

I\'m wondering why in the code below the i variable still shows \"5\" instead of showing \"1\" then \"2\" the

4条回答
  •  死守一世寂寞
    2021-01-25 23:53

    fiddle Demo

    var i = 0;
    
    function ajax_call() {
        $.ajax({
            url: '/echo/html/',
            method: 'post',
            data: {
                html: 'Ajax data'
            },
            success: function (resp) {
                $('#success').append(i++);
                if (i < 5) {
                    ajax_call();
                }
            }
        });
        $('#outsideAjax').append(i);
    };
    ajax_call();
    

提交回复
热议问题