Search code examples
javascriptjqueryjquery-load

looping for jquery load and wait until finished


I want to make a script that implement jquery to process php script that need a lot of execution time, so I split the process with jquery callback variable to process partially

but i don't know how to implement it. need help!

$("button").click(function(){
    i = 0;
    $("#div1").load("demo.php?i=" + i,function(){
        // when finished do jquery load "demo.php" again with i++ until i = 5
        i++;
    });
});

how to do that?


Solution

  • I would recommend using recursion and a callback function:

    $("button").click(function(){
        load(0);
    });
    
    function load(i){
        if(i < 6){
            $("#div1").load("demo.php?i=" + i,function(){
                load(++i);
            });
        }
    }