Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/41.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
访问全局变量javascript_Javascript_Node.js - Fatal编程技术网

访问全局变量javascript

访问全局变量javascript,javascript,node.js,Javascript,Node.js,我正在尝试使用nodejs和javascript下载一个网页。然而,它似乎有一个有限的循环。为什么? var downloadFinished = false; var downloadPage = function(url,file) { rest.get(url).on('complete', function(result) { if (result instanceof Error) { console.log('Error:', result.message);

我正在尝试使用nodejs和javascript下载一个网页。然而,它似乎有一个有限的循环。为什么?

var downloadFinished = false;

var downloadPage = function(url,file) {
  rest.get(url).on('complete', function(result) {
  if (result instanceof Error) {
    console.log('Error:', result.message);
  } else {
    fs.writeFileSync(file, result, 'utf8');
    downloadFinished = true;
   }   
  }); 
};

if(require.main == module) {
    downloadPage('http://google.com', 'new.html');
    while(!downloadFinished) {
       // wait till download finished.
    } 
    // do other stuff make sure download is finished to 'new.html'
}

当您调用
while(!downloadFinished)
时,它被设置为false,因此您基本上是在执行
while(true)

选择1 您可以使用回调而不是while循环

var successCallback = function() {
 //do stuff here.
};

var downloadPage = function(url, file, callback) {
  rest.get(url).on('complete', function(result) {
  if (result instanceof Error) {
    console.log('Error:', result.message);
  } else {
    fs.writeFile(file, result, 'utf8', callback);
   }   
  }); 
};

if(require.main == module) {
    downloadPage('http://google.com', 'new.html', successCallback);
}
选择2
看看,他们真的会帮你的。您可以使用一个很好的Promise库,只需添加到包依赖项中即可

当您在(!downloadFinished)时调用该
时,它被设置为false,因此您基本上是在
的同时(true)

选择1 您可以使用回调而不是while循环

var successCallback = function() {
 //do stuff here.
};

var downloadPage = function(url, file, callback) {
  rest.get(url).on('complete', function(result) {
  if (result instanceof Error) {
    console.log('Error:', result.message);
  } else {
    fs.writeFile(file, result, 'utf8', callback);
   }   
  }); 
};

if(require.main == module) {
    downloadPage('http://google.com', 'new.html', successCallback);
}
选择2
看看,他们真的会帮你的。您可以使用一个很好的Promise库,只需添加到包依赖项中即可

Javascript是单线程的,如果您有如下循环:

while(!downloadFinished) {

}
该循环将永远运行,没有其他函数将运行(您的
.on('complete'
回调在while循环完成之前无法执行,因为Javascript的单线程特性,所以它永远不会完成,因为您没有在该循环内设置
downloadFinished=true
,或者使用
break
语句)

要解决此问题,您可以在回调中执行所有其他操作,直到下载完成后才调用:

var downloadPage = function(url, file, callback) {
  rest.get(url).on('complete', function(result) {
    if (result instanceof Error) {
      console.log('Error:', result.message);
    } else {

      /* Don't use writeFileSync, unless you want to block your server,
        from handling any requests at all until the disk IO completes

        fs.writeFileSync(file, result, 'utf8');
        callback();
      */
      fs.writeFile(file, result, 'utf8', callback);
    }   
  }); 
};

if(require.main == module) {
    downloadPage('http://google.com', 'new.html', function after_download(){
        // do other stuff make sure download is finished to 'new.html'
    });
}

Javascript是单线程的,如果您有如下循环:

while(!downloadFinished) {

}
该循环将永远运行,没有其他函数将运行(您的
.on('complete'
回调在while循环完成之前无法执行,因为Javascript的单线程特性,所以它永远不会完成,因为您没有在该循环内设置
downloadFinished=true
,或者使用
break
语句)

要解决此问题,您可以在回调中执行所有其他操作,直到下载完成后才调用:

var downloadPage = function(url, file, callback) {
  rest.get(url).on('complete', function(result) {
    if (result instanceof Error) {
      console.log('Error:', result.message);
    } else {

      /* Don't use writeFileSync, unless you want to block your server,
        from handling any requests at all until the disk IO completes

        fs.writeFileSync(file, result, 'utf8');
        callback();
      */
      fs.writeFile(file, result, 'utf8', callback);
    }   
  }); 
};

if(require.main == module) {
    downloadPage('http://google.com', 'new.html', function after_download(){
        // do other stuff make sure download is finished to 'new.html'
    });
}

这是在循环时请求回调而不是无限。@你的意思是什么?传入一个回调函数,下载完成后可以调用该函数。类似于使用jquery的
$时的
成功
回调。ajax
()这是在循环时请求回调而不是无限。@你的意思是什么?传入一个回调函数,下载完成后可以调用该函数。类似于使用jquery的
$时的
成功
回调。ajax
()当它完成时,我实际上正在设置
downloadFinished=true
。你已经得到了我的投票,但请注意,OP提到了node.js。我不认为他们正在浏览器中运行它。我建议使用选项2,因为它真的是一个很棒的承诺库。@Paulpro Nice我实际上一直在为node寻找一个很好的承诺库。我哈哈我更新了我的答案以包含它!当它完成时,我实际上正在设置
downloadFinished=true
。你已经得到了我的投票,但请注意OP提到了node.js。我认为他们没有在浏览器中运行它。我建议使用选项2,因为它确实是一个很棒的Promise库。@Paulpro Nice我实际上一直在寻找一个node的承诺库很好。我已经更新了我的答案以包含它!@celebisait您最好使用
fs.writeFile
而不是
fs.writeFileSync
,所以我只是在我的答案中更改了它。
fs.writeFile
将文件写入文件系统后调用您的回调,并且您的服务器可以自由运行处理其他请求,因为它没有使用
synchronous
函数。@celebisait您最好使用
fs.writeFile
而不是
fs.writeFileSync
,因此我在回答中更改了这一点。
fs.writeFile
将文件写入文件系统后调用您的回调函数,并且您的服务器I它可以自由处理其他请求,因为它不使用
同步功能。