Javascript node.js中的node metainspetor获取未执行

Javascript node.js中的node metainspetor获取未执行,javascript,node.js,http,web,web-scraping,Javascript,Node.js,Http,Web,Web Scraping,我在Node.js中有一个简单的web抓取脚本,我正试图在循环中运行该脚本: var insp = require('node-metainspector'); var client = new insp("http://www.google.com", { timeout: 99999 }); var sleep = require("thread-sleep"); client.on("fetch", function(){ console.log("The title is:")

我在Node.js中有一个简单的web抓取脚本,我正试图在循环中运行该脚本:

var insp = require('node-metainspector');
var client = new insp("http://www.google.com", { timeout: 99999 });
var sleep = require("thread-sleep");

client.on("fetch", function(){
    console.log("The title is:");
    console.log(client.title);
});

client.on("error", function(err) {
    console.log(err);
});

while(true) {
    console.log("Checking...");
    client.fetch();
    sleep(10000);
}
当我在循环之外运行
client.fetch()
时,它工作得非常好,但只要它在循环中,它就不工作了。没有抛出错误(我用try-catch检查)。
循环中的其余行将执行。为什么会发生这种情况?我如何修复它?

线程睡眠模块使用同步API(
child\u process.execFileSync()
)来“睡眠”,这不是Node.js中推荐的方法。更适合使用异步API来实现您想要的。在您的情况下,一个简单的
setInterval
可以完成以下工作:

var insp = require('node-metainspector');
var client = new insp("http://www.google.com", { timeout: 99999 });

client.on("fetch", function(){
  console.log("The title is:");
  console.log(client.title);
});

client.on("error", function(err) {
  console.log(err);
});

setInterval(function() {
  console.log("Checking...");
  client.fetch();
}, 10000);

非常感谢。解决了的。为什么会这样?为什么同步睡眠会“杀死”它?JavaScript是一个单线程环境,任何同步操作都会停止整个编程的运行。这可能会导致意外行为。在99.9%的情况下,开发人员应该在node.js中使用异步操作