Sorry for asking, but how do I access myFunction() from someFunction()?
<script>
$(document).ready(function() {
// Get messages
var myFunction = function() {
// doSth.
}
// Make the initial call on page load.
myFunction();
);
function someFunction() {
// Call the function a second time
myFunction(); // Call fails!
}
</script>
Its more then a scope issue. Yes, you are scoping it to the anonymous function but even if you make it global by removing the var like this:
It STILL won’t work, because myFunction hasn’t been defined by the time you call someFunction. someFunction is running immediately before the document is ready, which is BEFORE myFunction is defined.
Both functions need to be either in the document ready block, or outside it. If you need to manipulate DOM elements, I’d recommend inside.
If
someFunctionis called for a handler, you can remove thevardeclaration frommyFunctionand it will work as expected, because this will putmyFunctionin the global scope.