Saturday, May 25, 2024
 Popular · Latest · Hot · Upcoming
5
rated 0 times [  12] [ 7]  / answers: 1 / hits: 71865  / 15 Years ago, fri, may 22, 2009, 12:00:00

I have created a Queue class in javascript and I would like to store functions as data in a queue. That way I can build up requests (function calls) and respond to them when I need to (actually executing the function).



Is there any way to store a function as data, somewhat similar to



.setTimeout(doSomething(), 1000);


except it would be



functionQueue.enqueue(doSomething());


Where it would store doSomething() as data so when I retrieve the data from the queue, the function would be executed.



I'm guessing I would have to have doSomething() in quotes -> doSomething() and some how make it call the function using a string, anyone know how that could be done?


More From » function

 Answers
87

All functions are actually variables, so it's actually pretty easy to store all your functions in array (by referencing them without the ()):



// Create your functions, in a variety of manners...
// (The second method is preferable, but I show the first for reference.)
function fun1() { alert(Message 1); };
var fun2 = function() { alert(Message 2); };

// Create an array and append your functions to them
var funqueue = [];
funqueue.push(fun1);
funqueue.push(fun2);

// Remove and execute the first function on the queue
(funqueue.shift())();


This becomes a bit more complex if you want to pass parameters to your functions, but once you've setup the framework for doing this once it becomes easy every time thereafter. Essentially what you're going to do is create a wrapper function which, when invoked, fires off a predefined function with a particular context and parameter set:



// Function wrapping code.
// fn - reference to function.
// context - what you want this to be.
// params - array of parameters to pass to function.
var wrapFunction = function(fn, context, params) {
return function() {
fn.apply(context, params);
};
}


Now that we've got a utility function for wrapping, let's see how it's used to create future invocations of functions:



// Create my function to be wrapped
var sayStuff = function(str) {
alert(str);
}

// Wrap the function. Make sure that the params are an array.
var fun1 = wrapFunction(sayStuff, this, [Hello, world!]);
var fun2 = wrapFunction(sayStuff, this, [Goodbye, cruel world!]);

// Create an array and append your functions to them
var funqueue = [];
funqueue.push(fun1);
funqueue.push(fun2);

// Remove and execute all items in the array
while (funqueue.length > 0) {
(funqueue.shift())();
}


This code could be improved by allowing the wrapper to either use an array or a series of arguments (but doing so would muddle up the example I'm trying to make).


[#99471] Wednesday, May 20, 2009, 15 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
luzv

Total Points: 178
Total Questions: 105
Total Answers: 114

Location: Palau
Member since Tue, May 30, 2023
1 Year ago
luzv questions
Thu, Aug 26, 21, 00:00, 3 Years ago
Mon, Nov 23, 20, 00:00, 4 Years ago
Thu, Jun 11, 20, 00:00, 4 Years ago
Wed, Apr 29, 20, 00:00, 4 Years ago
;