Been googling and looking for an answer to this / best practice.
Let’s say you have most of your javascript inside $(document).ready()
for whatever reason, and inside there you have a function that you want to "fire" based on some external javascript function..
What would be the best way to do that? Simply moving the function to the global space isn’t entirely a feasible option due to all the variables and stuff inside $(document).ready()
So in this example, there’s external javascript that does an ajax request, so when the request is completed, data get’s loaded on the page, and then I want to somehow be able to fire that function inside $(document).ready()
when that external javascript ajax completes, but I can’t simply call that function due to it not being in the global space.
2
Answers
you can use a setter to cast there:
this is quite powerful if you call the function before
$().ready
has completed it will wait until$().ready
runs. and if$().ready
is already running it will call the function tooUsage
in
$().ready
you have to callset('<function name>', <function>)
and when you want to call the function runrun('<function name>', ...<argumentts> )
If you do not want to expose a global then you can make an event that the code has to trigger which will inform when it is done.