0

This is my code:

var d = new Date();

function myFunction() {
  
  ifNewDay();
  
}

function ifNewDay() {
  var currentTime = d.toLocaleTimeString();
    if(currentTime == "3:21:00:00 AM EDT"){
      Logger.log("It is 3:21 PM");
    } else {
      ifNewDay();
    }
}

I am trying to write code that will only continue once it hits a certain time, I'm sure there's a way easier way to do this, but I'm brand new. Thanks.

sruly
  • 115
  • 9

2 Answers2

0

There's a limit on how deep you can recurse, and in you case, that's A LOT of calls in a short period.

When you call a function it gets added to the stack in memory, and there's a limit on how big the stack can be. You can read more here about how it works.

You could use sleep and call that function only every second or so, or at least half a second or something.

Community
  • 1
  • 1
LLL
  • 3,566
  • 2
  • 25
  • 44
  • Ahh, I had a hunch it had to do with recursing. Thanks for the answer. – sruly Apr 16 '17 at 19:49
  • No problemo! Btw, I'm not sure what you are using this for, but usually setting up some scheduled task in the system is a better way of doing something periodical like this, instead of checking if it's the time. – LLL Apr 16 '17 at 19:52
0

It seems like using a time driving trigger is what would work best. Have you looked into triggers? https://developers.google.com/apps-script/guides/triggers/installable

OblongMedulla
  • 1,471
  • 9
  • 21