4 Replies Latest reply on Jul 18, 2016 9:24 PM by msarabura RSS
    msarabura Newbie

    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?

      • Re: Passing calculated number to pause()
        carlesc Ninja

        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.

        • Re: Passing calculated number to pause()
          Sajid Patel Apprentice

          Try this:

           

          var maxDelay = 10;

          var minDelay = 1;

           

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

          pause(delay.toString());