Is there a way for me to delay a function call in Google Apps Scripts? I'm currently testing for smaller time frames, but I would eventually like to have a 72-hour wait period between processing data and calling moveRows.
I was trying to achieve this by making this Javascript function call:
setTimeout(function() { moveRows(arrayOfRows); }, 3000);
I also tried doing it with a trigger but my function never got called.
ScriptApp.newTrigger('moveRows(arrayofRows)')
.timeBased()
.everyMinutes(1)
.create()
What am I doing wrong?
As others have pointed out, setTimeout cannot be used in GAS. Instead, you can use the function [Utilities.sleep()][1] (see this answer).
setTimeout() belongs to the 'window' object that is not present in GAS. Remember, the code is compiled on Google servers, not in your browser, so you don't have access to DOM in this environment. Similarly, you can't reference 'document' or other DOM objects. The only place where it's possible is client-side HTML that HtmlService creates and sends to your browser for rendering.
You can only pass function name as parameter to the newTrigger() method. You are passing the parameter, which is why it doesn't work.
Related
Is there a way for me to delay a function call in Google Apps Scripts? I'm currently testing for smaller time frames, but I would eventually like to have a 72-hour wait period between processing data and calling moveRows.
I was trying to achieve this by making this Javascript function call:
setTimeout(function() { moveRows(arrayOfRows); }, 3000);
I also tried doing it with a trigger but my function never got called.
ScriptApp.newTrigger('moveRows(arrayofRows)')
.timeBased()
.everyMinutes(1)
.create()
What am I doing wrong?
As others have pointed out, setTimeout cannot be used in GAS. Instead, you can use the function [Utilities.sleep()][1] (see this answer).
setTimeout() belongs to the 'window' object that is not present in GAS. Remember, the code is compiled on Google servers, not in your browser, so you don't have access to DOM in this environment. Similarly, you can't reference 'document' or other DOM objects. The only place where it's possible is client-side HTML that HtmlService creates and sends to your browser for rendering.
You can only pass function name as parameter to the newTrigger() method. You are passing the parameter, which is why it doesn't work.
I want to use the Java API, HTMLUnit, to detect the number of eval() calls being called on the webpage by the JavaScript program. However, HTMLUnit doesn't have a built in handler for this type of JavaScript function. How can this be done?
Thanks.
Just an idea. Maybe you can inject a script with this code into the start of the html that you are loading:
(function(){
const oldEval = window.eval;
window.eval = function () {
// communicate here with your Java program that eval has been
// called. Maybe you can use the postMessage method?
return oldEval.apply(this, arguments);
};
})();
With this, you hijack the eval function and you can execute some code each time eval is called. If you figure out a good way to communicate back with your program then maybe this works.
Not sure if an issue or not, but Javascript has multiple ways to evaluate code during runtime, not just eval. So, this is hijacking a direct call to eval, but not considering other evaluation possibilities like using the Function constructor or setTimeout.
How can I go about getting an instance of a script that is loaded in Java Script?
I'm using the function load("script.js") and I need to call a function on that script, but I need the instance in a variable so i can store it in a map.
I need something like var script = load("script.js")
Then I can call script.unload() // a function defined in script.js
When you load a script, it's executed and any bindings it creates in the environment are created. You cannot unload a script. If you know what all of its bindings and other effects are, you could clear them all (for instance, if loading Underscore, you could do _ = undefined to clear that binding), but it's unlikely that all of the effects of loading the script will be undone.
(This isn't a Nashorn thing, it's a JavaScript thing. There's one environment shared by all loaded scripts. ES2015's modules help organize that better, but there's still just one overall environment.)
How can I check if a JavaScript function is fully executed without a callback function?
I am scripting for Adobe Illustrator and use the "save()" method to save my active Document. When it's done saving I want to do something, but only after its completely saved.
The method doesn't offer a callback function so I need another way to execute my own function, after the document is saved.
Which SDK do you use? Probably documentAfterSave event is what you need instead?
(described as dcoumentAfterSave (yes, with typo) in programmers-guide.pdf)
I am creating a userscript for a game that will modify certain parts of a page in real time to help the user know how long they must wait to perform certain actions.
The problem I am running into is that the game has some AJAX already built in, every three seconds it calls the jQuery.getJSON() function to grab information to update things. My script needs to make it appear to the end user as if the page was updating in real time, rather than every 3 seconds. As well as add extra information. Without adding extra requests (the games owners will not like that).
To do this I need to override the default behavior of the page, I need to change the callback function of the jQuery.getJSON() call to add my functionality. Or at least disable it completely so I can write a new one. And it isn't as easy as assigning a new function to the old name, as it has no name, they just build the function within the jQuery.getJSON() call. Is this possible?
The page script is contained in a separate .js file btw, if that makes any difference.
If the jQuery.getJSON() call is assigned to a variable, it will return a jqXHR object, which you can then modify by adding or changing its callbacks.
If it is not exposed as a variable, but instead is simply called like so
... js blah ...
jQuery.getJSON("myurl",function(){
more blah
});
... more blah ...
... then I believe you're up a creek without a paddle, as that becomes an anonymous function call with no handle. The only way, at that point, would be to try to override by loading another script in place over the first one, but I am really uncertain how stable that would leave the browser environment.
See the jQuery reference for http://api.jquery.com/jQuery.getJSON/ and http://api.jquery.com/Types/#jqXHR for more details on how the $.ajax() system works.