setInterval is not defined- Google Apps Script [duplicate] - javascript

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

Chrome Extension inject callback on webpage's native function call? [duplicate]

This question already has answers here:
Access variables and functions defined in page context using a content script
(6 answers)
Closed 5 months ago.
I have a Chrome Extension which runs on a dedicated domain and manipulates the DOM. (Everything here is kosher: the user INTENTIONALLY installs this for this specific given site, and even the site owners are aware/support this extension, but they are not able to change its code easily.)
One page loads the content via AJAX, and refreshes a given every minute or so with a polling AJAX call. There is a specific function, UpdatePageComplete() that they call at the end of the AJAX sync. I need that function to then call one of MY Chrome Extension functions, so that I can manipulate the new data (ex: adding <a href='tel:' wrappers to any phone numbers, or background-colors to elements with certain contents).
I can't figure out how to inject my own callback into their native functions though. Is this even possible? If so, where should I be looking?
I've done something similar in the past. You can't inject your own callback into their code, but you can:
Create a global function for them to call (inject window.mySpecialFn = () => {...})
Add a custom event listener on the body, and have their code dispatch that custom event.

Calling JS interops multiple times with Blazor

My server-side Blazor app calls multiple times a javascript function that is supposed to move a div around (using setInterval).
The issue is that the function does not wait for the previous call to finish. As a result, the DOM is changed from different js interops at the same time which leads to unpredictable results. I was expecting the calls to be stacked up and run one by one.
Do you have an idea how can I solve this issue? Thanks a lot!
Seems like the only way is to use custom data attributes on the DOM as a lock.
Something like that: data-lockincrement="0"
Each JSInterop call should include that lockincrement so Setinterval continues until lockincrement matches the value which means the calls will be executed sequentially.

Google Apps Scripts ReferenceError: "setTimeout" is not defined

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.

Check if function is fully executed

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)

Is there any way stopping or pausing the execution of Javascript in a JSContext object?

I'm developing an iOS 7 app that has scripting capabilities using JavascriptCore.
I'd like to have a way to pause or completely stop the code currently running on the JSContext. JavascriptCore isn't really well documented so I couldn't find an answer.
I have a few ideas:
Remove from the context the bridge object used to interact with my app and just let any code still running fail.
Getting the JSGlobalContextRef for my JSContext and releasing it using JSGlobalContextRelease and recreating a JSContext to use.
but hopefully there are better ways.
Any help would be appreciated. Thanks!
There actually is an API that allows to stop execution of Javascript code inside of JavascriptCore. Unfortunately though, it is private and thus cannot be used within apps that are to be distributed through the App Store.
The API in question is the JSContextGroupSetExecutionTimeLimit function defined in JSContextRefPrivate. Not only does it allow to specify a timeout for your JS code, but also a callback that will be called regularly to determine whether the execution should be stopped.
You can find more information in my answer to this SO question.
If still relevant, you could create a loop that adds the counter (that is going up after every cycle) and the maximum value of the counter. I assume you can use a second core since it is called javascriptcore. In this second core you can put a function that increases the value of the counter by 10 so it exceeds the max value of the counter and so the code will continue. You don't need a second core if you can call 2 functions at a time.
Otherwise you can store everything under a different name and overwrite the old ones with nothing.
u can use java script timeout callback when on JSContext object is triggered
function myFunction() {
alert('wait is over');
}
<!DOCTYPE html>
<html>
<body>
<button onclick="setTimeout(myFunction, 3000);">Click Me</button>
<!-- In this context im using a simple button to trigger the call back in your case u can triggeer on jscontext-->
</body>
</html>

Categories

Resources