gpt4 book ai didi

node.js - 如何在 Node.js 中每 2 秒打印 "Hello world"10 秒?

转载 作者:太空宇宙 更新时间:2023-11-03 22:41:10 27 4
gpt4 key购买 nike

我想以 2 秒到 10 秒的间隔打印一个值(如 Hello world)。我该怎么做?

最佳答案

使用setInterval()打印Hello World每 2 秒,使用 setTimeout() 10 秒后清除间隔。

var interval = setInterval(function(){ 
console.log('Hello World');
}, 2000);
setTimeout(function() {
clearInterval(interval);
}, 10000);

关于node.js - 如何在 Node.js 中每 2 秒打印 "Hello world"10 秒?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46337325/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com