0

I tried for several hours to get the following code working. The code should be paused until the page is loaded. The problem that I'm encountering is that the AJAX is executing asynchronously. How would I force the code to wait before executing?

var i = 1;
function On_Success(){

    i = i+1;
    if(i<=10){

    $('<div id="p'+i+'">b</div>').replaceAll('#b'); 
    $('#p'+i).load('tabelle.html');
    //here shoul be waited, til the page is loaded
    On_Success();
    };
    };

    function knopf(){
    $('body').append('<div id="p'+i+'">a</div>');
    $('#p'+i).load('tabelle.html'); 
    On_Success();
    };

3 Answers 3

2

Both Ajax and load have an on successs function that can be run when the response is fully returned.

$.ajax({
        async: true,
        type: "GET",
        url: url,
        data: params,
        success: function(html){
                 //do stuff here
                 },
        error: handleAjaxErrors

    });

If you want to use load, there is a success callback as well:

$("#myDiv").load('/ajax/data/get', function() {
  //do stuff here
});
Sign up to request clarification or add additional context in comments.

Comments

1

The load function has a success handler:

$('#p'+i).load('tabelle.html', function() {
  On_Success();
});

The success handler is only called after the ajax call completes successfully, and after the provided content has been added to the DOM.

You can see full documentation here:

http://api.jquery.com/load/

If you also want to capture error conditions on the client you will need to use the full $.ajax call as per @Chris's answer.

Comments

0

.load accepts a callback that runs when loading is complete; you can use this instead of the more verbose jQuery.ajax function, which will require you to write additional code to populate your element.

jQuery("#p", i).load("tabelle.html", function() {   
   //code to run when tabelle.html has finished loading
});

Change your code to the following. It should work:

var i = 1;

function On_Success() {

    i = i + 1;
    if (i <= 10) {
        $('<div id="p' + i + '">b</div>').replaceAll('#b');
        $('#p' + i).load('tabelle.html', On_Success);        
    };
};

function knopf() {
    $('body').append('<div id="p' + i + '">a</div>');
    $('#p' + i).load('tabelle.html', On_Success);
};

On another note, is it absolutely necessary that you should wait for one load to complete before populating the other divs? Why can't you do something like this:

function On_Success() {
    var i = 0;
    while(i < 11) {
        $('<div id="p' + i + '">b</div>').replaceAll('#b');
        $('#p' + i).load('tabelle.html');        
    };
};

Also, I'm not sure what the knopf function is doing. What should the value of i be in that function? How are you calling your code?

4 Comments

The original question asked for "page load" not for the load function. I've edited my answer.
I got stil the same problem, that the code goes on, without waiting
Now it´s working, I had remove the "()" after the On_Success call, do you know why?
@nadeshchda The reason is that load accepts a callback. When you add a () after On_Success, it actually calls the function. What you want to do is pass a reference instead, which is what you're doing when you omit the ().

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.