The problem of defining the function inside an anonymous function is that it will only belong to the context of this function, it will no longer exist when the anonymous function finishes executing. If the question is just to call the function inside the anonymous function that treats the done
event, you can define the desired function in a larger context and just call it, as the example below.
$(function () {
function minhaFuncao() {
alert("ok");
}
$.connection.hub.start().done(function () {
minhaFuncao();
});
});
In this way, minhaFuncao
is set in a larger context, existing in any part of the code within $(function () {...});
of jQuery.
The following example was only produced to show the above logic operation in the SOpt, since it would not be able to reproduce the $.connection.hub.start().done
event, since it depends on third-party plugins.
To see it working, just take a simpler example:
function minhaFuncao() {
alert("ok");
}
$(function () {
minhaFuncao();
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
The function itself defined as jQuery context is an anonymous function. Defining its function in the global context, it will exist there as well. When the page has the DOM loaded, an alert is displayed.