![](/img/trans.png)
[英]How can I ask the user for a word , and the program stops running when the user writes stop and the program shows all the words aside stop
[英]How can I prevent node program from stop running?
我看到许多使用节点的应用程序永远运行。
因此,我尝试使用setInterval
方法,该方法假定它可以使其永远运行,但显然不会。
var request = require('request');
var queue = function(item) {
request({
uri: 'http://www.google.net'
},
function(error, response, body) {
if (!error && response.statusCode == 200) {
console.log(body) // Print the google web page.
}
})
};
setInterval(queue("google"),1000); //do this every 1 second.
当我运行上面的程序时,它在一秒钟后停止。
如果使用node运行它,如何修改上面的代码以使其运行?
实际上,您的代码中存在一个错误:
setInterval(queue("google"),1000); //do this every 1 second.
而不是将上面的函数作为第一个参数传递,而是传递了执行函数的结果。
因此setInterval(queue, 1000)
如果需要多个参数setInterval(queue, 1000)
请执行setInterval(queue, 1000)
或执行以下操作:
setInterval(function() {
queue(how_many_params_you_want);
}, 1000);
更好的解决方案是使用npm: 永远
您必须将setInterval分配给局部变量。
setInterval(function(){},1000);
会停止。
var timer = setInterval(function(){},1000);
将不会。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.