【问题标题】:Inserting 'continue' statement within a function to be called later in another functions 'For loop' statement在函数中插入“继续”语句,稍后在另一个函数中调用“For循环”语句
【发布时间】:2019-10-09 11:13:10
【问题描述】:

为了简化我的代码,我制作了一个包含“if”语句的函数。该函数旨在在另一个函数的“For”循环中调用。我想在稍后调用的自定义函数中放置一个“继续”语句。有没有可能做到这一点而不会出错?代码是用 Google Apps 脚本编写的。

function example1() {
  //code here
  continue;
  }
}
        
function example2() { 
  for(var x = 1 ; x <= 2 ; x++) {
  example1();
  }
}

在 example1() 函数中调用“continue”语句时出现错误,因为“continue”语句不在“For”循环中。但是 example1() 函数仅创建为在 example2() 'For' 循环语句中调用。

【问题讨论】:

标签: javascript for-loop google-apps-script


【解决方案1】:

不,您必须在 for 循环内直接使用 continue。您可以让您的函数返回一个布尔值,然后根据需要执行 continue

function example1() {
  return shouldLoopContinue; // Add your own logic here
}

function example2() {
  for (var x = 1; x <= 2; x++) {
    if (example1()) {
      continue;
    }

    doSomeOtherWork();
  }
}

【讨论】:

    猜你喜欢
    • 2015-12-25
    • 2022-11-27
    • 1970-01-01
    • 2023-03-31
    • 1970-01-01
    • 2015-06-04
    • 1970-01-01
    • 1970-01-01
    • 2019-04-14
    相关资源
    最近更新 更多