【问题标题】:Angular js /Javascript keep track if how many times a process was calledAngular js /Javascript 跟踪进程是否被调用了多少次
【发布时间】:2019-03-13 09:03:53
【问题描述】:

我的代码在下面,根据 myArray.length 多次调用 doProcess,现在的问题是如何 我可以跟踪是否调用了多少次 doProcess ,我尝试使用计数器但似乎没有任何想法?不要担心doprocess被执行了,我已经做到了,我只想跟踪doprocess被调用了多少次。

doProcess(myArray, 0);
        function doProcess(myArray, index) {
            if (myArray.length === index) {
                return;
            }

            data["users"] = array
            data["random_code"] = myArray[index];

            QuestionaireService.share_questionaire(me, data).then(function (response) {

                if (response.status == "200") {
                    doProcess(myArray, ++index);
                    count++;
                }
            });

             console.log("count", count)

        }

【问题讨论】:

  • 您的count 变量在哪里声明?我猜在功能之外?你目前得到的输出是什么?
  • 退出函数
  • 多个计数,我只想最终计数不计数 1,计数 2 计数等等
  • 你试图在doProcess调用之后增加count,它永远不会增加,因为它是递归调用函数,在doProcess函数调用之前调用它
  • 把这个console.log("count", count)放到函数调用之外

标签: javascript angularjs function loops


【解决方案1】:

根据上述沟通。要求只是打印一次counter。将日志记录放在if 条件中。

试试这个代码。

var count = 0;

doProcess(myArray, 0);
    function doProcess(myArray, index) {
        if (myArray.length === index) {
            console.log("count", count)
            return;
        }

        data["users"] = array
        data["random_code"] = myArray[index];

        QuestionaireService.share_questionaire(me, data).then(function (response) {

            if (response.status == "200") {
                doProcess(myArray, ++index);
                count++;
            }
        });
    }

【讨论】:

  • 小心,如果您的share_questionaire 没有响应 200 代码,您将永远不会有 myArray.length === index,因此永远不要打印计数器。
【解决方案2】:

你为什么不尝试在你的函数之外设置一个变量,然后在里面增加它:

  let counter = 0
  doProcess(myArray, 0);

  function doProcess(myArray, index) {
    counter += 1
    if (myArray.length === index) {
      return;
    }

    data["users"] = array
    data["random_code"] = myArray[index];

    QuestionaireService.share_questionaire(me, data)
    .then((response) => {

      if (response.status == "200") {
        doProcess(myArray, ++index);
        count++;
      }
    });

    console.log("count", count)

  }

  console.log("counter", counter)

【讨论】:

    【解决方案3】:

    您的计数器增加得很好。但是既然你在doProcess函数内部使用了异步调用,那么问题是什么时候打印计数器?

    所以当你确定它不会增加更多时,你必须打印它(所以当你不再调用 doProcess() 时)。

    尝试类似:

    doProcess(myArray, 0);
    function doProcess(myArray, index) {
       count++;
       if (myArray.length === index) {
          // Need to print it too if the process is over
          console.log("count: ", count);
          return;
       }
    
       data["users"] = array
       data["random_code"] = myArray[index];
    
       QuestionaireService.share_questionaire(me, data).then(function (response) {
    
          if (response.status == "200") {
             doProcess(myArray, ++index);
          } else {
             // Here, you know you are in the last call of doProcess()
             console.log("count: ", count)
          }
       });
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-05-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-03
      • 1970-01-01
      • 2017-03-15
      • 1970-01-01
      相关资源
      最近更新 更多