如何将 javascript 函数存储在队列中以便最终执行

2022-01-21 00:00:00 function queue timeout javascript

我在 javascript 中创建了一个 Queue 类,我想将函数作为数据存储在队列中.这样我就可以构建请求(函数调用)并在需要时响应它们(实际执行函数).

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);

除非是

functionQueue.enqueue(doSomething());

它将 doSomething() 存储为数据的位置,因此当我从队列中检索数据时,将执行该函数.

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

我猜我必须在引号中加上 doSomething() -> "doSomething()" 以及如何让它使用字符串调用函数,有人知道怎么做吗?

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?

推荐答案

所有函数实际上都是变量,因此将所有函数存储在数组中实际上非常容易(通过在没有 () 的情况下引用它们):

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).

相关文章