Anthony TeslaX25
Anthony TeslaX25

Reputation: 75

wait for function complete and do other function

How can I wait for complete execution of a function and then call another function like

<script src="whereMyfunctionIs.js"></script>
<script>
    $(window).ready(start());
    function start (){
        myfunction().WhenFunctionDone(myOtherFunction());
    }
</script>

I wanna know if there is a method(js or jQuery) that can wait til myfunction completes, I've already used done, ready and load, an it doesn't wait.

Error messages:

jQuery.Deferred exception: Cannot read property 'ready' of undefined

jQuery.Deferred exception: Cannot read property 'load' of undefined

jQuery.Deferred exception: Cannot read property 'done' of undefined

Upvotes: 0

Views: 721

Answers (1)

Lincoln Bergeson
Lincoln Bergeson

Reputation: 3461

Try this:

<script>
$(window).ready(start());

function start (){
    $.when(myfunction()).done(myOtherFunction);
}
</script>

Here is the documentation for $.when.

Here is an explanation of Javascript's synchronous vs asynchronous execution.

Here is a similar question to yours.

Upvotes: 1

Related Questions