nightmare 运行 while 循环中的函数队列(让循环等待队列完成)
nightmare run function que in while loop (let the loop wait until que is finished)
我正在尝试 运行 在 while 循环中进行噩梦。我的问题是 while 循环没有等待噩梦结束。
这是我的示例代码:
var Nightmare = require('nightmare');
var Screenshot = require('nightmare-screenshot');
var i = 0;
while(i < 10) {
var nightmare = new Nightmare();
nightmare.goto('https:/website/?id='+ i);
nightmare.screenshot('/home/linaro/cointellect_bot/screenshot1.png');
nightmare.use(Screenshot.screenshotSelector('screenshot' + i + '.png', 'img[id="test"]'));
nightmare.run();
}
是否可以让循环等到 nightmare 完成它的函数队列?我还有哪些其他选择?
使用函数而不是循环:
var nightmare;
var Nightmare = require('nightmare');
var Screenshot = require('nightmare-screenshot');
var runNext = function (i) {
if (i < 10) {
nightmare = new Nightmare();
nightmare.goto('https:/website/?id='+ i);
nightmare.screenshot('/home/linaro/cointellect_bot/screenshot1.png');
nightmare.use(Screenshot.screenshotSelector('screenshot' + i + '.png', 'img[id="test"]'));
nightmare.run(function () {runNext(i+1);});
}
}
runNext(0);
nightmare.run
根据此文档接受回调:https://github.com/segmentio/nightmare#runcb
一旦噩梦结束或出错,作为参数传递的函数就会被调用。
这通常是 nodejs 中大多数异步事物的工作方式。
虽然您需要提取一个函数,但最好不要只传递一个数字,还要传递完整的上下文。因此,您的函数看起来像
var screenshotPage = function(data){
var nightmare = new Nightmare();
nightmare.goto(data.url);
nightmare.use(Screenshot.screenshotSelector(data.filePath, data.selector));
nightmare.run();
}
你应该能够运行像这样的例子
var Nightmare = require('nightmare');
var Screenshot = require('nightmare-screenshot');
var async = require('async')
var pages = [];
// You could do this recursively if you want
for(var i=0; i < 10; i++) {
pages.push({
url: 'https://website/?id='+ i,
filePath: 'screenshot' + i + '.png',
selector: 'img[id="test"]'
});
}
var screenshotPage = function(data, callback){
var nightmare = new Nightmare();
nightmare.goto(data.url);
nightmare.use(Screenshot.screenshotSelector(data.filePath, data.selector));
nightmare.run(function(){
callback(null);
});
}
async.map(pages, screenshotPage, function(){
// Here all screenshotPage functions will have been called
// there has been an error
});
我正在尝试 运行 在 while 循环中进行噩梦。我的问题是 while 循环没有等待噩梦结束。 这是我的示例代码:
var Nightmare = require('nightmare');
var Screenshot = require('nightmare-screenshot');
var i = 0;
while(i < 10) {
var nightmare = new Nightmare();
nightmare.goto('https:/website/?id='+ i);
nightmare.screenshot('/home/linaro/cointellect_bot/screenshot1.png');
nightmare.use(Screenshot.screenshotSelector('screenshot' + i + '.png', 'img[id="test"]'));
nightmare.run();
}
是否可以让循环等到 nightmare 完成它的函数队列?我还有哪些其他选择?
使用函数而不是循环:
var nightmare;
var Nightmare = require('nightmare');
var Screenshot = require('nightmare-screenshot');
var runNext = function (i) {
if (i < 10) {
nightmare = new Nightmare();
nightmare.goto('https:/website/?id='+ i);
nightmare.screenshot('/home/linaro/cointellect_bot/screenshot1.png');
nightmare.use(Screenshot.screenshotSelector('screenshot' + i + '.png', 'img[id="test"]'));
nightmare.run(function () {runNext(i+1);});
}
}
runNext(0);
nightmare.run
根据此文档接受回调:https://github.com/segmentio/nightmare#runcb
一旦噩梦结束或出错,作为参数传递的函数就会被调用。
这通常是 nodejs 中大多数异步事物的工作方式。
虽然您需要提取一个函数,但最好不要只传递一个数字,还要传递完整的上下文。因此,您的函数看起来像
var screenshotPage = function(data){
var nightmare = new Nightmare();
nightmare.goto(data.url);
nightmare.use(Screenshot.screenshotSelector(data.filePath, data.selector));
nightmare.run();
}
你应该能够运行像这样的例子
var Nightmare = require('nightmare');
var Screenshot = require('nightmare-screenshot');
var async = require('async')
var pages = [];
// You could do this recursively if you want
for(var i=0; i < 10; i++) {
pages.push({
url: 'https://website/?id='+ i,
filePath: 'screenshot' + i + '.png',
selector: 'img[id="test"]'
});
}
var screenshotPage = function(data, callback){
var nightmare = new Nightmare();
nightmare.goto(data.url);
nightmare.use(Screenshot.screenshotSelector(data.filePath, data.selector));
nightmare.run(function(){
callback(null);
});
}
async.map(pages, screenshotPage, function(){
// Here all screenshotPage functions will have been called
// there has been an error
});