apparently there is something in my code that is stuck as on occasions it causes the script to run until time out at 6minutes.
I am still trying to find that code but in the meantime, i really need to prevent the waiting. My script typically needs 10 seconds only.
Is there a way for me to set such that any script that hits 10 seconds should be terminated.
much appreciated!
CodePudding user response:
First of all, isn't it better to rewrite the code so that it does not reach 6 minutes?
If there is a possibility that it will exceed 6 minutes, try using a trigger to execute it for more than 6 minutes.
CodePudding user response:
If there are multiple commands or is executed repeatedly, then TheMaster's recommendation in the comments is a good thing to be considered.
This will how it would look like.
function sampleFunction() {
var startTime = Date.now();
// do your thing, initialization, etc.
while (true) {
// do your thing, main process
// if 10 seconds is done
if ((Date.now() - startTime) >= 10000)
return;
}
}
Limitations:
- If the command before the if condition checking the time takes 5 minutes, then you will be able to exit the script after that execution which will be 5 minutes later.
- You can't stop a single command mid-execution. You need to wait for your if condition to be executed.
Things to note:
- Exiting script through the method above works best when numerous lines of codes are being repeated in a loop and consistently checking the if condition so you can exit the script when the time comes.
- You can only exit the script in between commands, not during those commands. So using this between commands that take too much time might stop the script later than expected.
- It is still best to debug what is happening and why that "BUG" happens, or maybe you missed something that is causing the unexpected behavior.
Upon providing the exact code, the community will be able to help you with a more specific answer. Until then, speculations could only be provided.