Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 805
anmml Avatar asked Oct 21 '25 18:10

anmml


2 Answers

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

like image 191
Amit Agarwal Avatar answered Oct 23 '25 08:10

Amit Agarwal


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>
like image 43
turtlefranklin Avatar answered Oct 23 '25 08:10

turtlefranklin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!