javascript - async遍历的时候怎么延迟执行呢
黄舟
黄舟 2017-04-10 16:48:20
[JavaScript讨论组]

在node中使用,用npm安装了async;
我先说一下流程,我首先遍历获取一个url数组

http://w/page/1
http://w/page/2
http://w/page/3
http://w/page/4

在遍历这个数组,每个url又会生成一个数组,

[http://w/998.html,
http://w/997.html,
http://w/996.html,
http://w/995.html,
http://w/994.html,
http://w/993.html,
http://w/992.html,
http://w/991.html,
http://w/990.html,
http://w/989.html]...

然后遍历数组里面每个url
下面我用伪码描述一下

for (var i = 1; i < 3; i++) {
    ihx.push('http://w/page/' + i);
}
self.ihx(ihx);

    ihx: function (arr) {
        var self = this;
        async.each(arr, function (item, callback) {
            setTimeout(function () {
                console.log(item)
                self.core(item, function ($) {
                    var $itemlist = $('.post-listing .item-list');
                    var linkList = $itemlist.map(function (idx, element) {
                        var $element = $(element).find('.post-title a');
                        return $element.attr('href');
                    }).get();
                    console.log("linkList===" + linkList)
                    async.each(linkList, function (item, callback2) {
                        setTimeout(function () {
                            self.core(item, function ($) {
                                var author = $('.post-inner .entry').find('p').eq(0).text();
                                var content = $.html('.article_text');
                                var text = $('.post-inner span[itemprop="name"]').text();
                                console.log("text=" + text);
                                if (!!text) {
                                    var thor = new articlema({
                                        title: text,
                                        author: author,
                                        category: '002',
                                        content: content
                                    });
                                    thor.save(function (err, thor) {
                                        if (err) return console.log(err);
                                        //console.log(thor);
                                    });

                                }
                            });
                        }, 2000)


                    }, function (err) {
                        console.log("content==============" + err);
                    });

                })
            }, 1000)
        }, function (err) {
            console.log("item==============" + err);
        })
    }

这个没有达到我想的结果 console.log(linkList),是先打印的page/2页面抓取的url,时间也没有慢下来,并且外面的循环完了,才会循环里面的,这个我应该怎么改一下

黄舟
黄舟

人生最曼妙的风景,竟是内心的淡定与从容!

全部回复(1)
迷茫

自己控制每个异步操作的执行

ihx: function (arr) {
        var self = this;
        var copyiedarr=arr.slice(0);

        function next() {
            var item = copyiedarr.shift();
            console.log(item);
            if (!item) {
                console.log('end~~');
            }
            self.core(item, function ($) {
                var $itemlist = $('.post-listing .item-list');
                var linkList = $itemlist.map(function (idx, element) {
                    var $element = $(element).find('.post-title a');
                    return $element.attr('href');
                }).get();
                console.log("linkList===" + linkList);

                nextLinkeList(linkList, function () {
                    setTimeout(function(){
                        next();
                    },1000);
                });
            });
        }

        function nextLinkeList(linkList,doneCallback){
            var copyiedarr=linkList.slice(0);

            function next(){
                var item=copyiedarr.shift();
                console.log(item);
                if(!item){
                    doneCallback();
                }
                self.core(item, function ($) {
                    var author = $('.post-inner .entry').find('p').eq(0).text();
                    var content = $.html('.article_text');
                    var text = $('.post-inner span[itemprop="name"]').text();
                    console.log("text=" + text);
                    if (!!text) {
                        var thor = new articlema({
                            title: text,
                            author: author,
                            category: '002',
                            content: content
                        });
                        thor.save(function (err, thor) {
                            if (err) return console.log(err);
                            //console.log(thor);
                        });

                    }

                    setTimeout(function(){
                        next();
                    },2000);
                    
                });
            }
            
            next();
        }
        
        next();
    }
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号