1

Here's my jquery function, I'm stuck in a circular callback functions. I'm wondering how do I convert this code to call itself? or is it even possible?

$('#content').load('myurl/items', function() {
    ...
    ...
    $(this).find('form').submit(function(e) {
        e.preventDefault();
        $.ajax({
            type: 'POST',
            url: 'myurl/items',
            context: document.getElementByID('content'),
        }).done(function() {
            $(this).load('/myurl/items', function(){}); // This is recursive
        });    
    }
});
1
  • What are you trying to do? Commented Apr 23, 2015 at 2:49

1 Answer 1

1

Declare a function:

function loadItems() {
    $('#content').load('myurl/items', function() {
        ...
        ...
        $(this).find('form').submit(function(e) {
            e.preventDefault();
            $.ajax({
                type: 'POST',
                url: 'myurl/items',
                context: document.getElementByID('content'),
            }).done(function() {
                loadItems(); // Call the function to continue
            });    
        }
    });
}

loadItems(); // Call the function to start

Of course, the loop will end if the ajax call is ever unsuccessful.

Sign up to request clarification or add additional context in comments.

Comments

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.