首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >多个实例不会退出

多个实例不会退出
EN

Stack Overflow用户
提问于 2013-10-30 18:06:13
回答 1查看 572关注 0票数 0

我有一个casperjs代码,如下所示:

代码语言:javascript
复制
var links = [
    { url : "http://engadget.com/" , name : "eng" },
    {url : "http://shoryuken.com/2013/02/01/help-save-super-arcade/",name: "shir"},
    {url: "http://theverge.com/",name:"thever" }
];

var eng_done = false , shir_done=false, thever_done=false;
var casper = require('casper');
for(i=0;i<links.length;i++){
    var url = links[i].url;
    var name = links[i].name;
    var instance_i = casper.create();
    instance_i.start(url,function(){
        console.log("Loading: "+ name);
        name_done = true;

    });
    instance_i.run(function(){

        exit(i);
    });

}


function exit(idz){

        console.log("Now exiting instance no => "+idz);
        if(eng_done & shir_done & thever_done){
            idz.exit();
        }

}

你可以清楚地看到,我正在启动3个实例,并同时退出它们,问题是,由于出口(I)总是被塞到3,所以退出这些实例是不恰当的。我不知道我在这里做错了什么……(拜托,这些URLS只是样本)。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-11-01 12:02:46

所以我终于找到了我的问题的答案,把它发到这里,这样总有一天它会帮到某个人:)我知道代码有点不合标准,但它确实有效,所以我对它没有意见:)

代码语言:javascript
复制
var view_width = 1024, view_height = 768;
var clip_width = 1366, clip_height = 768;
var settings = {
pageSettings : {
    loadImages : true,
    loadPlugins : false
}   ,
 timeout : 120000,
 // // Set the viewport size
 viewportSize: {width: view_width, height: view_height},
 // // Set the position and the size of the clipped image
 clipRect: { 
  top: 0, 
  left: (view_width - clip_width)/2, 
  width: clip_width, 
  height: clip_height 
 },verbose: true, logLevel: 'debug'
};
var links = [
    { url : "http://google.com" , name : "google" },
    {url : "http://bing.com",name: "bing"},
    {url: "http://facebook.com/",name:"facebook" }
];
var casper = require('casper');

currentLink = 0;
for (var i=0, item; item=links[i]; i++) {
  // item is "some", then "example", then "array"
  // i is the index of item in the array
  var name = item.name;
 var instance_i = casper.create({logLevel : "debug",
 verbose : true});
 captureTheImage(instance_i,item.url,item.name,i);

}

function captureTheImage(i,u,n,e){
    var google_done=false,
    bing_done=false,
    facebook_done=false;
    i.start(u,function(){
    // item.name_done = true;
    this.capture("c:/name_"+e+"_"+n+".jpg",undefined,{
            format: 'jpg',
        quality: 75
        });
     n_done = true; 
    });
    i.clear();
    i.run(function(){

    console.log("Links done : "+currentLink);
    currentLink++;
    if(currentLink == 3){
        this.exit();
    }

    });     

}

希望它能帮助到某人:)

另外,如果添加了更多的链接,则可以将其值传递给captureTheImage,然后在其中指定if(currentLink==),使用links.length作为for循环定义当前数组的长度。;)

最好的

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19690591

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档