javascript - 如何在nodejs中重复执行

标签 javascript node.js npm

基本上,我想将 console.log 重复 5 次 我使用 repeating - npm 这听起来可能非常基本,但我无法弄清楚如何使用它来简单地重复 console.log('test'); 5次,使用我需要的 nodejs这适用于应在 nodejs 中多次重复特殊代码的 Node 应用程序。希望这里有人可能知道解决方案。

app.js

const repeating = require("repeating");

repeating(5, console.log('test'));

最佳答案

您使用repeating 打印重复字符串:

const repeating = require('repeating');

console.log(repeating(100, 'unicorn '));

检查 DOCS . repeating(count, [string]) 函数只接受一个字符串。


要多次调用一个函数,您可以使用 for 循环。

for (var i = 0; i < 5; i++) {
  console.log("Hi");
}

上面的代码就像下面的一行for循环

for (var i = 0; i < 5; i++) console.log("Hi");

你可以有递归。

(function repeat(number) {
  console.log("Hi");
  if (number > 1) repeat(number - 1);
})(5);

这个comment中还有更多例子.

关于javascript - 如何在nodejs中重复执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55080650/

相关文章:

Node.js 对 HTTPS 返回空响应

javascript - 将点击事件和参数绑定(bind)到div?

javascript - 忽略其中包含特定单词的模式

javascript - 如何将整个 npm 库导入到 React Native 中,而不仅仅是 1 个文件?

node.js - 执行 npm start 时出现 Azure Functions Node 错误

javascript - 无法使用 webpack 在 GitHub Actions 中为生产构建

javascript - 使用 Sanity npm 包开 Jest 模拟测试

javascript - 在另一个图像之上加载图像

node.js - 在 AWS Beanstalk 上使用 node.js 监听 SSL

node.js - 将 npm 包与相等的 devDependencies 和 peerDependencies 链接会破坏使用 webpack 进行开发的应用程序