这是低效的,我该如何循环呢?

This is inefficient, how can I loop instead?

我正在尝试提取两个动词及其相关语句,这段代码有效,但我发现它效率低下。我尝试将动词放在一个数组中,然后在命名动词的每一行中引用数组项,但这引发了错误。我可以使用什么代码来通过我构建的动词数组进行循环?谢谢。

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title>Get 2 verbs</title>
        <script src="xapiwrapper.min.js"></script>
    </head>
    <body>
        <div id='demo'></div>
        <script>

                var text = "";
                ADL.XAPIWrapper.changeConfig({"endpoint":"https://lrs.adlnet.gov/xapi/"});            
                var search = ADL.XAPIWrapper.searchParams();
                search['verb'] = ADL.verbs.completed.id;
                search['since'] = "2017-08-08T08:34:16Z";
                var res = ADL.XAPIWrapper.getStatements(search);
                if (res) {
                    text = JSON.stringify(res);
                    }              

                var text2 = "";
                ADL.XAPIWrapper.changeConfig({"endpoint":"https://lrs.adlnet.gov/xapi/"});            
                var search = ADL.XAPIWrapper.searchParams();
                search['verb'] = ADL.verbs.resumed.id;
                search['since'] = "2017-08-08T08:34:16Z";
                var res = ADL.XAPIWrapper.getStatements(search);
                if (res) {
                    text2 = JSON.stringify(res);
                    }   

            document.getElementById('demo').innerHTML = text + text2;
        </script>
    </body>
</html>

也许是这样的?当然未经测试。

var configs = [
  {
    endpoint: 'https://lrs.adlnet.gov/xapi/',
    verb: ADL.verbs.completed.id,
    since: "2017-08-08T08:34:16Z"
  },
  {
    endpoint: 'https://lrs.adlnet.gov/xapi/',
    verb: ADL.verbs.resumed.id,
    since: "2017-08-08T08:34:16Z"
  }
];

var out = '';
configs.forEach(function(cfg) {
  ADL.XAPIWrapper.changeConfig({"endpoint":cfg.endpoint});
  var search = ADL.XAPIWrapper.searchParams();
      search['verb'] = cfg.verb;
      search['since'] = cfg.since;
  var res = ADL.XAPIWrapper.getStatements(search);
  if (res) {
    out += JSON.stringify(res);
  }
});

console.log(out);

如果您询问是否可以 return 在单个请求中使用任一动词的语句列表,标准 XAPI 搜索不支持。您将需要发出多个请求并在最后合并两组语句。