Josh
Josh

Reputation: 1

How to add a pause/delay during if else statements

I am looking to slow down my semi-automated javascript code. Currently it pulls detailed information of 100 listings one at a time at a rate of about 15 times a second. This is good in some ways but causes issues tracking which element goes with which listing in the array. This will be hard to explain, so bear with me and I will try my best.

The code displays a button next to each item on a webpage. upon the first button press it collects detailed data relating to the listing then displays text within the button in the form of 0.#### then moves onto the next one.

Now it's probably my poor coding causing issues (forgive me, I'm very new) but when the value is below 0.006 it does some stuff (explained in snippit) then performs a secondary .click. This secondary click takes around 4 seconds to process, so I would like the code to wait for this to finish before moving onto the next item within the list (the final piece of code in the snippit does this).

I've made notes in the snippit and hopefully it is clear!

// Code excerpt, assume all vars are set here along with the onclick to call this snippit

// If below 0.0010 it will reduce var 'message' to 4 points after decimal, add 'var2' 
// (marked irrelevant) text onto the 'message' variable then click .element2 based off 
// of the contents of 'var1'
// As an example, 'message' is 0.0006. add var2 (great) you have a button showing 
// [great 0.0006]. Then it will interact with the button beside it [.element2]
    	   if(message < 0.0010)    {  
              message = message.toFixed(4);
              message = irrelevant var2 + message;
              $("#" + irrelevant var + " .element1 span").text(message);
           document.getElementsByClassName("element2")[var1].click(); 
            }

// If below 0.006 it will reduce var 'message' to 4 points after decimal, add 'var3' 
// (marked irrelevant) text onto the 'message' variable then click .element2 based off 
// of the contents of 'var1', similar to the first if statement
            else if(message < 0.006)    {  
              message = message.toFixed(4);
              message = irrelevant var3 + message;
              $("#" + irrelevant var + " .element1 span").text(message);
           document.getElementsByClassName("element2")[var1].click(); 
            }

// If below 0.008 it will reduce var 'message' to 4 points after decimal, add 'var4' 
// (marked irrelevant) text onto the 'message' variable then +1 to 'var1'
// This range doesn't interact with another button, it adds 1 to var1 that tells it to
// skip this instance of the .element2 button so it doesn't click the wrong line
           else if(message < 0.008)    {  
              message = message.toFixed(4);
              message = irrelevant var4 + message;
                function var1Count (){
                var1++;}
                var1Count();
              $("#" + irrelevant var + " .element1 span").text(message);
            }

// If greater than 0.008 it will reduce var 'message' to 3 points after decimal, then +1 
// to 'var1'
// This range doesn't interact with another button, it adds 1 to var1 that tells it to
// skip this instance of the .element2 button so it doesn't click the wrong line
           else if(message > 0.008)    {
              message = message.toFixed(3);  
                function var1Count (){
                var1++;}
                var1Count();
            $("#" + irrelevant var + " .element1 span").text(message); 
                
            }
            
 function testCount (){
                count++;
            }
            testCount();

// This starts the process again but on the next item in the list using the variable count           
	    $(".myButton").click (something);
           document.getElementsByClassName("myButton")[count].click();

Upvotes: 0

Views: 234

Answers (1)

bcherny
bcherny

Reputation: 3172

For this sort of automation, something like selenium is nice because of the automatic synchronization and sleep() method.

In pure javascript, what you want is the setTimeout function.

Looking at your code, it's not obvious why you need this timeout at all. Without a more complete example, it's hard to be more helpful.

Upvotes: 1

Related Questions