setTimeout函数上缺少形式参数

时间:2019-09-09 18:33:34

标签: javascript google-apps-script google-places-api settimeout

我目前正在用Google Apps脚本编写脚本,以获取符合我的文字搜索条件的特定位置的商店列表。我试图设置超时功能,以便在我尝试从Google Places API接收结果的下一页时,由于某种原因,它一直告诉我error: setTimeout function is missing a formal parameter。我已经在线查看了所有内容,并且将其包含在代码中的方式与执行该功能的所有其他方式相似。如果有人可以帮助我,那就太好了!谢谢!

这是我的 setTimeout函数


        function setTimeout( function(){
          while(tempPage != null){

            count = count++;
            var nxtUrl = "https://maps.googleapis.com/maps/api/place/textsearch/json?pagetoken=" + tempPage + "&location=41.661129,-91.530167&radius=8050&key=" + apiKey;
            var tempResponse = UrlFetchApp.fetch(nxtUrl);
            var tempJson = tempResponse.getContentText();
            Logger.log(tempResponse);

            tempPage = JSON.parse(tempJson).next_page_token;

            Logger.log("Page count: " + count);

            for(var j = 0; j < 20; j++){

              var tempPlace = JSON.parse(tempJson).results[j];

              Logger.log("Name: " + tempPlace.name);
              Logger.log("Address: " + tempPlace.formatted_address);
            }// end for loop

          }// end while loop
        }, 3000);

1 个答案:

答案 0 :(得分:0)

Google Apps脚本不使用setTimeout函数。解决方案是改用Utilities.sleep()函数。


        Utilities.sleep(3000);

          while(tempPage != null){

            count = count++;
            var nxtUrl = "https://maps.googleapis.com/maps/api/place/textsearch/json?pagetoken=" + tempPage + "&location=41.661129,-91.530167&radius=8050&key=" + apiKey;
            var tempResponse = UrlFetchApp.fetch(nxtUrl);
            var tempJson = tempResponse.getContentText();
            Logger.log(tempResponse);

            tempPage = JSON.parse(tempJson).next_page_token;

            Logger.log("Page count: " + count);

            for(var j = 0; j < 20; j++){

              var tempPlace = JSON.parse(tempJson).results[j];

              Logger.log("Name: " + tempPlace.name);
              Logger.log("Address: " + tempPlace.formatted_address);
            }// end for loop

          }// end while loop

相关问题