Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/475.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 有没有办法在谷歌应用程序脚本中创建每秒增加一分钟的for循环?_Javascript_Google Apps Script_Timer_Spreadsheet_Google Docs Api - Fatal编程技术网

Javascript 有没有办法在谷歌应用程序脚本中创建每秒增加一分钟的for循环?

Javascript 有没有办法在谷歌应用程序脚本中创建每秒增加一分钟的for循环?,javascript,google-apps-script,timer,spreadsheet,google-docs-api,Javascript,Google Apps Script,Timer,Spreadsheet,Google Docs Api,最近,我一直在从事一个项目,该项目将数据从谷歌文档实时传输到谷歌电子表格。我一直在玩应用程序脚本API,并使用基于时间的触发器方法创建了一个每分钟更新一次的循环。我还知道,应用程序脚本不允许持续时间超过5分钟的for循环。所以,我的理论是触发器每分钟调用的函数可以有一个循环运行大约一分钟。问题是,我不能让一个函数每秒运行一次,甚至一分钟内每2秒或3秒运行一次。下面的代码是我用来创建这个循环的代码,但是我无法让它工作。它要么会在一两秒钟内快速浏览数字,要么会持续运行,永不停止 function m

最近,我一直在从事一个项目,该项目将数据从谷歌文档实时传输到谷歌电子表格。我一直在玩应用程序脚本API,并使用基于时间的触发器方法创建了一个每分钟更新一次的循环。我还知道,应用程序脚本不允许持续时间超过5分钟的for循环。所以,我的理论是触发器每分钟调用的函数可以有一个循环运行大约一分钟。问题是,我不能让一个函数每秒运行一次,甚至一分钟内每2秒或3秒运行一次。下面的代码是我用来创建这个循环的代码,但是我无法让它工作。它要么会在一两秒钟内快速浏览数字,要么会持续运行,永不停止

function myfunction() {
  let start = new Date().getTime()
  const limit = 60;
  let timeEllapsed = 0
  
  for(i=0; i < 60; i++) {
    let start = new Date().getTime()
    while(true){
     if(start > new Date().getTime + 1000) {
      Logger.log(timeEllapsed)  
      timeEllapsed += 1
      break
   }
  }
}
}
如何修复此问题,使for循环每秒仅重复一次,并且脚本将在60秒后完成运行?

应用程序脚本有一个将暂停代码的休眠方法:

function myfunction() {
  let start = 0;
  var timeEllapsed = 0;
  
  const limit = 60000;
  const increment = 1000;//1000 milliseconds is 1 second
  
  while(timeEllapsed < limit){
    timeEllapsed += increment;
    Utilities.sleep(1000);//Wait for 1 second
  }
  
  Logger.log(timeEllapsed);
}
应用程序脚本有一个将暂停代码的睡眠方法:

function myfunction() {
  let start = 0;
  var timeEllapsed = 0;
  
  const limit = 60000;
  const increment = 1000;//1000 milliseconds is 1 second
  
  while(timeEllapsed < limit){
    timeEllapsed += increment;
    Utilities.sleep(1000);//Wait for 1 second
  }
  
  Logger.log(timeEllapsed);
}
60秒计时器:

HTML:

60秒计时器:

HTML:

function onSecond(t) {
  const ss=SpreadsheetApp.getActive();
  const sh=ss.getActiveSheet();
  sh.getRange(1,1).setValue(t);
}

function showTimerDialog() {
  SpreadsheetApp.getUi().showSidebar(HtmlService.createHtmlOutputFromFile('ah1'));
}