Sleep function in server side script?

ishtein
Mega Contributor

In my server side script, I am triggering an action via an outbound ReST call. The asynchronous operation returns immediately, but provides a URL which I can poll for the operation's completion. I would like to keep the function atomic, so would like to poll the URL, and if the operation hasn't yet completed, would like to wait for a second or so and poll again, and do that for several times (I do not expect the operation to take more than 10-20 seconds, normally not more than 5 seconds) until done.

I am looking for something like setTimeout or setInterval function that I could use on the server, so that I can tell my script to wait, but can't find anything. Am I missing something? Can somebody point me to a solution for this kind of problem?

I have seen a timer activity in Workflow, which would pause the workflow for a specified period of time; it can probably serve as a workaround, but I wanted a programmatic (and programmable) solution. Will be grateful for any guidance!

17 REPLIES 17

Thank you! It works. This is not in the documentation and the link to the wiki page is no longer available.

Watch out! The instance becomes unusable while using gs.sleep and running the script in background script. It seems to be putting the whole instance to sleep!

Goran WitchDoc
ServiceNow Employee
ServiceNow Employee

There is an undocumented function gs.sleep(milliseconds) that perhaps can help you here.


Göran,


As I noted in the thread with Chuck Tomasi, I have a scoped application and can't use gs.sleep(). Thanks for the advice anyway!


Any other possibilities I should consider?


I've seen this workaround, never tested it thou.. a selfmade sleep function:



  1. /* time is in milliseconds - this is essentially a "sleep" function */  
  2. wait: function(time){  
  3.   var t1 = new GlideDateTime().getNumericValue();  
  4.   var t2 = new GlideDateTime().getNumericValue();  
  5.   var duration = t2 - t1;  
  6.   while(duration < time){  
  7.   t2 = new GlideDateTime().getNumericValue();  
  8.   duration = t2 - t1;  
  9.   }  
  10. },  


from here: gs.sleep() in scoped application?