The Zurich release has arrived! Interested in new features and functionalities? Click here for more

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

@nirwan_ritik 

can you post a new question for this and share your complete business requirement and script you tried?

Tag me there as this is an old thread.

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

Mahesh23
Mega Sage

Hi,

you can try below code

var delayInMilliseconds = 10000; //10 seconds

setTimeout(function() {
  //your code to be executed after 10 seconds
}, delayInMilliseconds);

Max_MS
Tera Contributor

How did this work for you? For me it said

Evaluator: com.glide.script.RhinoEcmaError: "setTimeout" is not defined.