1. <legend id='dAEFe'><style id='dAEFe'><dir id='dAEFe'><q id='dAEFe'></q></dir></style></legend>

    <i id='dAEFe'><tr id='dAEFe'><dt id='dAEFe'><q id='dAEFe'><span id='dAEFe'><b id='dAEFe'><form id='dAEFe'><ins id='dAEFe'></ins><ul id='dAEFe'></ul><sub id='dAEFe'></sub></form><legend id='dAEFe'></legend><bdo id='dAEFe'><pre id='dAEFe'><center id='dAEFe'></center></pre></bdo></b><th id='dAEFe'></th></span></q></dt></tr></i><div id='dAEFe'><tfoot id='dAEFe'></tfoot><dl id='dAEFe'><fieldset id='dAEFe'></fieldset></dl></div>
  2. <small id='dAEFe'></small><noframes id='dAEFe'>

      <bdo id='dAEFe'></bdo><ul id='dAEFe'></ul>
  3. <tfoot id='dAEFe'></tfoot>

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

      时间:2023-09-29
      • <bdo id='3zY5o'></bdo><ul id='3zY5o'></ul>

      • <i id='3zY5o'><tr id='3zY5o'><dt id='3zY5o'><q id='3zY5o'><span id='3zY5o'><b id='3zY5o'><form id='3zY5o'><ins id='3zY5o'></ins><ul id='3zY5o'></ul><sub id='3zY5o'></sub></form><legend id='3zY5o'></legend><bdo id='3zY5o'><pre id='3zY5o'><center id='3zY5o'></center></pre></bdo></b><th id='3zY5o'></th></span></q></dt></tr></i><div id='3zY5o'><tfoot id='3zY5o'></tfoot><dl id='3zY5o'><fieldset id='3zY5o'></fieldset></dl></div>

        1. <legend id='3zY5o'><style id='3zY5o'><dir id='3zY5o'><q id='3zY5o'></q></dir></style></legend>
          <tfoot id='3zY5o'></tfoot>

            <small id='3zY5o'></small><noframes id='3zY5o'>

                  <tbody id='3zY5o'></tbody>
              • 本文介绍了如何将 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).

                这篇关于如何将 javascript 函数存储在队列中以便最终执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:如何在 JavaScript 中实现堆栈和队列? 下一篇:如果 Javascript 不是多线程的,是否有任何理由实现异步 Ajax Queuing?

                相关文章

                  <small id='bKhhq'></small><noframes id='bKhhq'>

                  <i id='bKhhq'><tr id='bKhhq'><dt id='bKhhq'><q id='bKhhq'><span id='bKhhq'><b id='bKhhq'><form id='bKhhq'><ins id='bKhhq'></ins><ul id='bKhhq'></ul><sub id='bKhhq'></sub></form><legend id='bKhhq'></legend><bdo id='bKhhq'><pre id='bKhhq'><center id='bKhhq'></center></pre></bdo></b><th id='bKhhq'></th></span></q></dt></tr></i><div id='bKhhq'><tfoot id='bKhhq'></tfoot><dl id='bKhhq'><fieldset id='bKhhq'></fieldset></dl></div>

                  <legend id='bKhhq'><style id='bKhhq'><dir id='bKhhq'><q id='bKhhq'></q></dir></style></legend>
                1. <tfoot id='bKhhq'></tfoot>
                    <bdo id='bKhhq'></bdo><ul id='bKhhq'></ul>