首页 > web前端 > js教程 > 正文

如何将JavaScript函数存储在队列中并按顺序执行?

王林
发布: 2023-08-24 18:05:02
转载
858人浏览过

如何将javascript函数存储在队列中并按顺序执行?

有时,开发人员可能需要将函数存储在队列中,并按照存储在队列中的顺序执行它。在JavaScript中,我们可以使用数组来创建一个队列。我们可以使用数组的push()方法将函数入队列,并使用shift()方法从队列中出队函数。

Below, we will see examples of storing JavaScript functions in the queue and executing them in the queue order.

Syntax

用户可以按照以下语法将JavaScript函数存储在队列中,并按顺序执行。

while (queue.length > 0) { 
   queue[0](); 
   queue.shift(); 
}
登录后复制

我们使用上述语法中的while循环来遍历队列。我们执行队列中的第一个函数,然后将该函数从队列中移除。

立即学习Java免费学习笔记(深入)”;

Example

在下面的示例中,我们创建了一个名为queue的变量,并用空数组初始化它,使其成为一个队列。

After that, we created three different functions and used the push() method of the array to add all functions into the queue. Whenever users press the button, it calls the executeFunctions() function. In the executeFunctions() function, we use the while loop to deque the function from the queue. In every iteration of the loop, we execute the first function from the array and use the array.shift() method to remove the first element from the array.

<html>
<body>
   <h3>Using the array to add functions in Queue and execute functions in particular order</h3>
   <div id = "content"> </div></br>
   <button onclick = "executeFunctions()"> Execute function in queue order </button>
   <script>
      let content = document.getElementById("content");
      // execute the functions in the order they are added to the queue
      var queue = [];
      function executeFunctions() {
         while (queue.length > 0) {
            queue[0]();
            queue.shift();
         }
      }
      function function1() {
         content.innerHTML += "Function 1 executed <br>";
      }
      function function2() {
         content.innerHTML += "Function 2 executed <br>";
      }
      function function3() {
         content.innerHTML += "Function 3 executed <br>";
      }
      queue.push(function1);
      queue.push(function2);
      queue.push(function3);
   </script>
</body>
</html>
登录后复制

Example

在下面的示例中,我们创建了一个数组,用作队列,就像第一个示例一样。之后,我们将sum()、sub()、mul()和div()函数添加到队列中。

在executeFunctions()函数中,我们使用for循环按照队列顺序调用所有函数。此外,我们还使用了call()方法从队列中调用函数。

<html>
<body>
   <h3>Using the array to add functions in Queue and execute functions in particular order</h3>
   <div id = "content"> </div> </br>
   <button onclick = "executeFunctions()"> Execute function in queue order </button>
   <script>
      let content = document.getElementById("content");
      // execute the functions in the order they are added to the queue
      var queue = [];
      function executeFunctions() {
         for (let i = 0; i < queue.length; i++) {
            queue[i].call();
         }
      }
      let a = 10;
      let b = 5;
      function sum() {
      content.innerHTML += "Sum of " + a + " and " + b + " is " + (a + b);
      }
      function sub() {
         content.innerHTML += "<br>Subtraction of " + a + " and " + b + " is " + (a - b);
      }
      function mul() {
         content.innerHTML += "<br>Multiplication of " + a + " and " + b + " is " + (a * b);
      }
      function div() {
         content.innerHTML += "<br>Division of " + a + " and " + b + " is " + (a / b);
      }
      queue.push(sum);
      queue.push(sub);
      queue.push(mul);
      queue.push(div);
   </script>
</body>
</html>
登录后复制

用户学会了将函数存储在队列中,并按照队列顺序执行它们。在JavaScript中,没有内置的队列数据结构,但我们可以使用数组作为队列。

In the first example, we have used the shift() method to deque the queue. In the second example, we have used the for loop to execute the functions in order.

以上就是如何将JavaScript函数存储在队列中并按顺序执行?的详细内容,更多请关注php中文网其它相关文章!

java速学教程(入门到精通)
java速学教程(入门到精通)

java怎么学习?java怎么入门?java在哪学?java怎么学才快?不用担心,这里为大家提供了java速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载
来源:tutorialspoint网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号