I need to add a sleep function within a scheduled ...
# ask-stanley-ai
p
I need to add a sleep function within a scheduled script. Is that possible? The script executes a call out to another system that has API limits so I need to delay 5 seconds between each request.
c
AI generated answer (using internal Slack threads):
****** Disclaimer: This response was generated by an artificial intelligence assistant based on previous conversations from multiple Slack channels, and may contain inaccuracies or mistakes. ****** Yes, it is possible to add a sleep function within a scheduled script in NetSuite, but it's not straightforward due to the instruction count limit of SuiteScripts. One approach is to create a Promise-based sleep function like this:
Copy code
javascript
const sleep = (milliseconds) => {
  return new Promise(resolve => setTimeout(resolve, milliseconds))
}
However, SuiteScript does not expose
setTimeout
, and this approach could hit the instruction count limit. Another approach is to perform some I/O operations, such as loading a record or a file from the filing cabinet, to slow down the script. But the time spent will be highly variable and it's not a recommended approach. A more reliable method is to use an external service that responds after a predefined delay. For example, you can use an HTTP echo service like this:
Copy code
javascript
https.get({url: "<https://httpbin.org/delay/1.6>"})
This will cause your script to wait for the specified delay before proceeding. Please note that these are workarounds and may not be the best solution for your use case. It's recommended to re-architect your solution to avoid the need for a sleep function if possible.
Slack thread 1: here (available only if the original thread was posted in the last 90 days)
Similarity score: 0.863194048
Slack thread 2: here (available only if the original thread was posted in the last 90 days)
Similarity score: 0.854132
Slack thread 3: here (available only if the original thread was posted in the last 90 days)
Similarity score: 0.844922602
Please provide your feedback on this answer.