素材牛VIP会员
async.queue 在kill 之后,无法第二次执行对列,前端妹子刚用async.queue
 素材牛  分类:Node.js  人气:900  回帖:2  发布于6年前 收藏

async.queue 点击事件,kill()执行后,执行async,queue,点击第二次,kill()不执行,也不执行async.queue,怎么回事
就是这个库

讨论这个帖子(2)垃圾回帖将一律封号处理……

Lv4 码徒
那***哦 移动开发工程师 6年前#1

代码如下:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<title>爬虫小测试</title>
<meta name="description" content="">
<meta name="keywords" content="">
<script src="./jquery-2.2.1.min.js"></script>
<script src="./async.min.js"></script>
<style>
    * {
        padding: 0;
        margin: 0;
        box-sizing: border-box;
    }
    .container {
        width: 500px;
        margin: 30px auto;
        text-align: center;
    }
    .result {
        border: 1px solid #9E9E9E;
        height: 300px;
        overflow: auto;
        margin-bottom: 30px;
    }
    .btn {
        color: white;
        font-weight: 300;
        font-size: 16px;
        background-color: #7B72E9;
        border-color: #7B72E9;
        text-decoration: none;
        text-align: center;
        line-height: 40px;
        height: 40px;
        padding: 0 40px;
        margin: 0;
        border-radius: 200px;
        display: inline-block;
        appearance: none;
        cursor: pointer;
        border: none;
        -webkit-box-sizing: border-box;
        -moz-box-sizing: border-box;
        box-sizing: border-box;
        -webkit-transition-property: all;
        transition-property: all;
        -webkit-transition-duration: .3s;
        transition-duration: .3s;
    }
</style>
</head>
<body>
    <div class="container">
        <div class="result">
            
        </div>
        <div class="btn" id="scratch">
            抓取
        </div>
    </div>

    <script>
        $(function () {
            // create a queue object with concurrency 2
             var q = async.queue(function(task, callback) {
                  console.log('hello ' + task.name);
                  callback('some thing wrong');
              }, 2);

             // assign a callback
             q.drain = function() {
                  console.log('all items have been processed');
              };

            $('#scratch').click(function() {
                // add some items to the queue
                q.push({name: 'foo'}, function(err) {
                    if(err) {
                        console.log(err);
                    } else {
                        console.log('finished processing foo');
                    }
                });

                q.push({name: 'bar'}, function (err) {
                    if(err) {
                        console.log(err);
                        q.kill();
                    } else {
                        console.log('finished processing bar');
                    }
                });

                q.push({name: 'meng'}, function (err) {
                    if(err) {
                        console.log(err);
                    } else {
                        console.log('finished processing meng');
                    }
                });
            });
        });
    </script>
</body>
</html>
Lv4 码徒
酒***无 PHP开发工程师 6年前#2

直接给你api。或许你会找到你要的答案,看看kill()做的事情

 文明上网,理性发言!   😉 阿里云幸运券,戳我领取