anmml
anmml

Reputation: 263

Google Scripts - Creating a script that runs every seconds

I am trying to create a script that runs every 10 seconds in google scripts, but I haven't found any method to do that,

My Code is running every minute:

  ScriptApp.newTrigger("refresh") 
  .timeBased()
  .everyMinutes(1)
  .create();

I've tried this javascript's setInterval method:

setInterval(function(){ /*do*/ }, 10000);

However, set interval is not defined.

Thanks for any help.

Upvotes: 4

Views: 1417

Answers (2)

turtlefranklin
turtlefranklin

Reputation: 529

Any JS written for Google Apps Script is executed server side. Because it is a free service, they make it difficult to take much server-side compute time. But because you can trigger these scripts from the browser, if all that you're writing is an add-on, then write the timer in your html files, as they will be executed by the client.

For instance, in my code.js script file I might have the following function:

function appendHello() {
    DocumentApp.getActiveDocument().getSelection().appendText("Hello! ");
}

And in my html for my add-on (with imported JQuery), I've placed the setInterval method there instead:

<script>
    $(function() {
        setInterval(function() { google.script.run.appendHello(); }, 5000);
    });
</script>

Upvotes: 1

Amit Agarwal
Amit Agarwal

Reputation: 11278

Time based triggers only let you run scripts at 1 minute intervals and, unfortunately, that cannot be changed.

Upvotes: 3

Related Questions