I have created a basic Google Apps Script that links to IFTTT on my phone and sends SMS messages. Now, I am attempting to create a more user-friendly interface by deploying the script as a Google Web App. The concept is simple: click a button, see "Sending SMS," and wait until the process is completed.
I believe the button code needs to check if the script is running or not (i.e., terminated), but I am struggling to make it work.
Here is how I have set things up:
In code.gs, I have the main function responsible for sending SMS messages:
var isRunning;
function execute() {
isRunning = true;
if (checkRunning() == true) {Logger.log('isRunning: TRUE')} else {Logger.log('isRunning: FALSE')}
OTHER STUFF;
isRunning = false;
if (checkRunning() == true) {Logger.log('isRunning: TRUE')} else {Logger.log('isRunning: FALSE')}
}
and the function that checks whether the variable isRunning
is true or false:
function checkRunning() {
if (isRunning == true) {
return true}
else {return false}
}
In my index.html file, there is a button to run the script and another to check its status:
<input class="button" type="button" value="Send SMS" onclick="google.script.run.execute()"/>
<input class="btncheck" type="button" value="Check running"/>
with this code within the <script></script>
tags (using Jquery):
var c;
setInterval(function() {
google.script.run.withSuccessHandler(varSet).checkRunning()
},1000);
function varSet(value) {c = value}
$('.btncheck').on('click', function() {
alert();
});
function alert() {
alert(c)
}
I know that the execute()
function runs for approximately 15 seconds. So, if I click the "Execute" button, I should be able to click the "Check running" button shortly after and receive a response of true. However, it always displays false because the checkRunning()
function returns false.
In the script Log, though, the isRunning
variable is correctly set to true at the start and false at the end of execute()
.
Any suggestions?