Community Tip - Did you get an answer that solved your problem? Please mark it as an Accepted Solution so others with the same problem can find the answer easily. X
The following code throws Error JavaException: java.lang.Exception: Invalid Pause Value : For input string: "2267.0"
var delay = 0;
delay = parseInt(Math.round(1000 * ((maxDelay - minDelay) * Math.random() + minDelay)));
pause(delay);
No matter what I try - parseInt, Number - I can't convert the delay into an argument that the pause function will accept.
On the other hand, pause(2267) works just fine where 2267 is hard coded.
How can I request a programmatically-generated delay?
Solved! Go to Solution.
Try this:
var maxDelay = 10;
var minDelay = 1;
var delay = Math.round(1000 * ((maxDelay - minDelay) * Math.random() + minDelay));
pause(delay.toString());
Try putting the pause(XXX) inside a service, and that service has a parameter of type LONG, pass the calculation to this services instead directly to the pause.
But take it easy, as pause only supports pause for 1minute ( 59999 ), you can't do a bigger pause.
For instance I have a long running pause implementation like this one:
Things["UtilsHelpers"].pause({ milliseconds: 100000 });
pause code:
if (milliseconds>=60000) {
var nTimes = parseInt(milliseconds/60000,10);
for(var i=0;i<nTimes;i++) pause(59999);
// -- As the following instruction doesn't works and it should: case: 12808137
// -- we will just trigger half a minute more
//pause(parseInt(milliseconds%60000,10));
pause(30000);
} else {
pause(milliseconds);
}
As you can see it's an approximation when it's > 1minute.
But by the way, if you need pause > 1 minute, better you implement a queue system, pause() blocks the thread and the consumed memory.
I've mostly ended not using it at all, and using my own queue system.
Try this:
var maxDelay = 10;
var minDelay = 1;
var delay = Math.round(1000 * ((maxDelay - minDelay) * Math.random() + minDelay));
pause(delay.toString());
I was just curious why pause() was not accepting a number which was obviously an 'int', so I tried out the above snippet and it works. This answer above does not take into consideration one minute limitation of pause(), if the required pause is longer than a minute, the answer from Carles is more appropriate.
This is the closest to what I need since my delays are typically less than one second.