I have an array of strings that I wish to use as callbacks, but the below code is not working.
When running the function, I’m getting the error TypeError: fn is not a function logged when callback_array contains only update_front_page_images.
callback_array currently only contains 1 element (update_front_page_images), which is the name of a function I wish to run.
Can someone please let me know what I am doing wrong?
function run_reset_callbacks(callback_array){
for(var key in callback_array){
try {
fn = window[callback_array[key]];
fn();
}
catch(err) {
console.log('Function \''+callback_array[key]+'\' does not exist. '+err);
}
}
}
Make sure you’re defining your functions in a way that you can call them as window[] if you want to use this model. You need to define your functions as variables on the window object. A standard function definition will fail.
http://jsfiddle.net/HpXYM/2/
This should solve the problem if you are set on this method but I would recommend you follow Matt Ball’s advice.