cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
Showing results for 
Search instead for 
Did you mean: 

Community Tip - When posting, your subject should be specific and summarize your question. Here are some additional tips on asking a great question. X

Passing calculated number to pause()

msarabura
5-Regular Member

Passing calculated number to pause()

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?

ACCEPTED SOLUTION

Accepted Solutions

Try this:

var maxDelay = 10;

var minDelay = 1;

var delay = Math.round(1000 * ((maxDelay - minDelay) * Math.random() + minDelay));

pause(delay.toString());

View solution in original post

4 REPLIES 4

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.

msarabura
5-Regular Member
(To:SajidPatel)

This is the closest to what I need since my delays are typically less than one second.

Announcements


Top Tags