为什么即使在pop()中完成数组后Javascript仍继续运行

时间:2020-02-25 04:25:41

标签: javascript shopify greasemonkey

我正在尝试使用TamperMonkey自动执行一些按钮单击和将数据插入网页的操作。但是,即使已插入数组中的所有数据,javascript仍会继续单击按钮。我如何设置它,以便一旦数组中的数据完成插入后,javascript应该停止运行。 这是用于自动将礼品卡批量输入shopify的代码。

// ==UserScript==
// @name         Shopify Gift Card Generator
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  Generate Gift Cards from a list
// @author       You
// @grant    GM.getValue
// @grant    GM.setValue
// @require http://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js
// @match        https://[myshopname].myshopify.com/admin/gift_cards
// @run-at      document-idle

// ==/UserScript==

/* globals $ */

(function() {
    'use strict';

    //add recipient list
    var rec = ["2920469463099",
                 "2920466415675",
            ];

    function generateCard(code){

        if($('button:contains("Issue gift card")') !== "undefined"){

            //click generate gift card
            $('button:contains("Issue gift card")').click();

            //set recipient based on id
            $('#gift_card_customer_id').attr("value", code);

            //select the dollar amount eq(0) = 10, eq(1) = 25, eq(2) = 50, eq(3) = 100
            $('.segmented-control.large li:eq(2) label')[0].click();

            //submit!
            //$('form.new_gift_card')[0].submit();
            $('input[type=submit]').click();

            var success = false;
            function closeWindow(){

                if($('.gift-card-success-heading h2:contains("RM50.00 MYR gift card successfully issued")').length == 1){
                    if ($('#gift-card-issue-errors:contains("There ")').length == 0) {
                        //close the modal and restart!
                        $('a.close-modal').click();
                        console.log('last successful recipient id: ');
                        console.log(GM.getValue('last'));

                        setTimeout(function(){
                            generateCard(rec.pop());
                        }, 750);

                    } else {

                        console.log('generation error. last code:');
                        console.log(GM.getValue('last'));
                        success = false;
                    }
                } else {

                    setTimeout(function(){ closeWindow(); }, 500);
                }
            }

            closeWindow();
            return success;

        } else {
            generateCard(code);
        }
    }

    function begin(){

        //wait for the page to load / not over-request the shopify server
        setTimeout(function(){
            generateCard(rec.pop());


        }, 1500);

    }

    begin();

})();

关于如何解决此问题的任何建议?谢谢。

3 个答案:

答案 0 :(得分:1)

rec为空时不要启动下一个计时器。

if (rec.length > 0) {
    setTimeout(function(){
        generateCard(rec.pop());
    }, 750);
}

答案 1 :(得分:1)

在setTimeout中调用generateCard方法之前添加检查。
当前,在数组为空之后,将使用undefined值调用此方法。

setTimeout(function() {
  if (rec.length > 0) {
    generateCard(rec.pop());
  }
}, 1500);

答案 2 :(得分:0)

对于每个数组项,您可以在函数内的代码中使用reducer method。这样,当您完成数组执行的最后一项时,就停止了。

let yourArray = [...yourData];
const reducer = (accumulator, currentValue) => {
    //your button clicking logic here
};

yourArray.reduce(reducer);