I am trying to pass one function into another so that I can call it when an ajax call has finished, but I get an error saying that the function I want to call is not a function.
File1 (Loaded first)
function functionOne() {
//Some Code
}
File2 (Loaded second and contains ajax call)
function functionTwo(functionOne) {
functionOne();
}
functionTwo();
I get the console error of TypeError: functionOne is not a function
My question is two fold:
- Why is
functionOneout of scope in the second function? - Is this the best way of ensuring the ajax call has finished before running my first function code?
functionTwo?functionOnethat shadows the global. "2. Is this the best way of ensuring the ajax call has finished before running my first function code?" Depends what you mean. You're not really showing what you're doing.functionOneinsidefunctionTwo. The problem isfunctionTwodefines a parameter with the same name asfunctionOne. The identifier name resolution is such that the variable/parameter closest to its point of use will be used. Therefore, you're using the function parameter because it blocks (shadows) the global with the same name. And because you're not passing anything to that parameter, it'sundefined, which isn't a function. Just ditch that function parameter and it'll work.