Search code examples
google-apps-scriptinfinite-loop

Program seems to hang only when NOT debugging in Google Apps Script


In the script I've created, when I run it normally, seems to hang or get stuck or end up in an infinite loop. I placed a couple of breakpoints throughout my code to step through it to try find where the problem is, but when I run it in the debugger, there aren't any issues!

I've dug around online about this a little, but can't find anything. Probably more of an issue of not putting in the correct search criteria, but in any event, I'm stuck.

Ok, so I think I've found the region that the problem lies in. Here is the code:

function inductDaycareIntoSystem(){
  var ss = SpreadsheetApp.getActive();
  var controlRoomSheet = ss.getActiveSheet();  // alias for master doc sheet
  var daycareNumber = chooseADaycare();  // Holds the number entry for the daycare chosen to work on (the starting entry, when doing multiple daycares) col. A
  Logger.log("Line 5");
  var finalDaycareNumber = daycareNumber; //  Holds the last daycare slot number to be processed (same number as 'daycareNumber' when only one is being processed)
      //Needs to be adjusted to get a final number as well

  for(var i = daycareNumber; i >= finalDaycareNumber; i ++){

     **code to execute in loop**
  }

The program still seems to hang with all the code inside the for loop commented out.


Solution

  • Think about what you are doing

    • Let's assume daycareNumber = 2
    • Since var finalDaycareNumber = daycareNumber; - finalDaycareNumber will also be 2
    • For your loop it means: for(var i = 2; i >= 2; i ++){
    • In words: Start with i =2 and increase iin every iteration as long asiis not smaller than2`
    • But i will never be smaller than 2, since 2 is the start value and from there on i will only increase in each iteration