View Full Version : creating array of functions

tyler jones
Mar 16th, 2007, 06:52 PM
I have a page that uses ajax. The main call is called by a number of different methods that each need something different to happen when the ajax call finishes. The cleanest way I could think of to solve this was to create an array of functions that needed called so that in my main ajax call, when it's done, it iterates through each function in the array. When I try to put the function in the array, I get a "Cannot assign to a function result" error. Here's my code. Is it possible to create an array of functions and if so, what am I doing wrong? Thanks.

function AddCleanUpRoutine(func){
var i = CleanUpRoutines.length;
if (i > 0)
CleanUpRoutines(i) = func; //errors here with Cannot assign to a function result

tyler jones
Mar 16th, 2007, 07:08 PM
Nevermind, I realized that I had () around my indexer instead of [].

tyler jones
Mar 16th, 2007, 07:51 PM
Now I'm running into a problem where when I loop through my array of functions, it will only execute the first one. In this code, it gives me 4 for the length, but it only ever alerts me to function 0 being executed. The following 3 never execute. If I comment out the line that says func = CleanUpRoutines[i]();, I get alerted 0, 1, 2, 3 for all of the positions of the array. Is there another way I should be trying to call these functions? Thank.s

for (i = 0; i < CleanUpRoutines.length; i++){
func = CleanUpRoutines[i]();