Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

need Delay function for script

sierra2
Mega Expert

Hi,

can anyone please tell how can I introduce a specified time delay between two commands.

example

I need an alternative of Sleep command. 

var max_retries = 10; //5 minutes, 30 seconds apart for each try
var max_retries_counter = 0;
var employeeID = "";


    //Check if the employeeID is valid
    if (employeeID.charAt(0) == "X") {
        break; 
    } else {
        max_retries_counter ++;
        sleep(30000); //30 seconds
    }
}

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

I have used this in past and it works well

delay of 10 seconds

var seconds = parseInt(10, 10) * 1000;

var start = parseInt(new Date().getTime()) + seconds;

while(start>parseInt(new Date().getTime())){

// do nothing

}

OR

ms -> milliseconds

function sleep(ms) {
  var endSleep = new GlideDuration().getNumericValue() + ms;
  while ( new GlideDuration().getNumericValue() < endSleep) {
   //wait 
  }

  return;
}

Regards
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

View solution in original post

7 REPLIES 7

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

I have used this in past and it works well

delay of 10 seconds

var seconds = parseInt(10, 10) * 1000;

var start = parseInt(new Date().getTime()) + seconds;

while(start>parseInt(new Date().getTime())){

// do nothing

}

OR

ms -> milliseconds

function sleep(ms) {
  var endSleep = new GlideDuration().getNumericValue() + ms;
  while ( new GlideDuration().getNumericValue() < endSleep) {
   //wait 
  }

  return;
}

Regards
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Thank you Ankur this is working 

Ho do you modify it, for example to have only 5 sec delay?

Hi Ankur, 

I want to use this in a script include. Does it have a bad impact on ServiceNow? Should we use it or go with any other methods to incorporate delay?

Regards