CasperJS从我的for循环中仅多次发布最后一个项目 [英] CasperJS posting only the last item multiple times from my for-loop

查看:100
本文介绍了CasperJS从我的for循环中仅多次发布最后一个项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

CasperJS很棒,但是它并没有将我的控制台输出发布到我的本地主机.

CasperJS is awesome but it's not posting to my localhost what my console output is.

casper.wait(5000, function () {
    casper.wait(1000, function () {
        casper.then(function(){
            for  (var i = 0 ; i < 10; i++) {
                var description = casper.fetchText(x('//*[@id="acDataId-local'+i+'"]/a')); //*[@id="acDataId-local0"]/a
                console.log(description);
                var target_date = casper.fetchText(x('//*[@id="dtDataId-local'+i+'"]/text()[1]')); 
                console.log(target_date);
                var target_location = casper.fetchText(x('//*[@id="veDataId-local'+i+'"]')); 
                console.log(target_location);

                console.log(i, description)

                casper.then(function () {

                    casper.open('http://localhost:1337/events', {
                        method: 'post',
                            data:   {
                            'description': description,
                            'target_date':  target_date,
                            'target_location':  target_location,
                        },
                        headers: {
                           "stuff":"stuff"
                        }
                    });
                });
            }
            this.echo('POST ' + i );
        });
    });
});


casper.run();

Console.log恰好在我需要的时候输出,但它仅发布了最后一个项目.我试图在多个地方添加casper.wait,但似乎没有帮助!

Console.log is outputting exactly when I want but it is only posting the last item. I tried to add casper.wait in a variety of places but it didn't seem to help!

推荐答案

CasperJS中的所有then*wait*函数都是异步的步函数 JavaScript具有函数级作用域.

All then* and wait* functions in CasperJS are asynchronous step functions and JavaScript has function-level scope.

这意味着for循环将立即执行,并且计划在for循环完全完成后执行几个then()步骤.届时,函数级变量descriptiontarget_date等都将具有最后一个i的值,而i的值将为10.这是一个通用的JavaScript示例:循环内的JavaScript封闭-简单的实际示例

This means that the for-loop is executed immediately and several then() steps are scheduled to be executed after the for-loop is completely finished. At that point the function-level variables description, target_date, etc. will all have the value of the last i and i will be 10. Here is a general JavaScript example of that: JavaScript closure inside loops – simple practical example

您可以

  • 将两个调用casper.then()casper.open()更改为单个调用casper.thenOpen(),将循环变量直接传递给该函数:

  • change the two calls casper.then() and casper.open() to a single call casper.thenOpen() where the loop variables are passed to the function directly:

casper.thenOpen('http://localhost:1337/events', {
    method: 'post',
        data:   {
        'description': description,
        'target_date':  target_date,
        'target_location':  target_location,
    },
    headers: {
       "stuff":"stuff"
    }
});

通过引入IIFE

  • 或关闭"每次迭代的变量:

  • or "close" the variables for each iteration by introducing an IIFE:

    for  (var i = 0 ; i < 10; i++) {
        (function(){
            var description = casper.fetchText(x('//*[@id="acDataId-local'+i+'"]/a')); //*[@id="acDataId-local0"]/a
            console.log(description);
            var target_date = casper.fetchText(x('//*[@id="dtDataId-local'+i+'"]/text()[1]')); 
            console.log(target_date);
            var target_location = casper.fetchText(x('//*[@id="veDataId-local'+i+'"]')); 
            console.log(target_location);
    
    
            console.log(i, description)
    
    
            casper.then(function () {
    
                casper.open('http://localhost:1337/events', {
                    method: 'post',
                        data:   {
                        'description': description,
                        'target_date':  target_date,
                        'target_location':  target_location,
                    },
                    headers: {
                       "stuff":"stuff"
                    }
                });
            });
        })();
    }
    

  • 这篇关于CasperJS从我的for循环中仅多次发布最后一个项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆