Trying to optimize my semi-automated JavaScript code so it runs at a slower pace. Currently, it fetches detailed data from 100 listings one by one at a speed of around 15 times per second. While this works well in some aspects, it makes tracking which element corresponds to each listing in the array difficult. Allow me to elaborate on this challenge.
The code places a button next to each item on a webpage. Upon the first button press, it gathers specific data related to the listing and displays text within the button as 0.#### before moving on to the next one.
It might be due to my inexperienced coding skills (please excuse me, I'm still learning), but when the value drops below 0.006, certain actions are triggered (explained in the snippet) followed by a secondary .click that takes roughly 4 seconds to execute. Ideally, I want the code to pause until this process finishes before proceeding to the next item on the list (the final section of code in the snippet addresses this).
I've included notes in the snippet to hopefully clarify things!
// Code snippet - assuming all variables are defined here and an onclick is set to call this snippet
// When below 0.0010, 'message' will be rounded to 4 decimal points, 'var2'
// (considered irrelevant) text added to 'message', then .element2 clicked based on 'var1' content.
// For instance, if 'message' is 0.0006, add var2 (great) resulting in the button showing
// [great 0.0006]. Then interact with the adjacent button [.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 under 0.006, round 'message' to 4 decimal points, add 'var3'
// (considered irrelevant) text to 'message', then click .element2 based on
// 'var1' content, 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();
}
// When below 0.008, round 'message' to 4 decimal points, add 'var4'
// (marked irrelevant) text to 'message', and increment 'var1'.
// This range does not involve interacting with another button, it increments var1
// to prevent clicking the wrong line on the .element2 button.
else if(message < 0.008){
message = message.toFixed(4);
message = irrelevant var4 + message;
function var1Count (){
var1++;
}
var1Count();
$("#" + irrelevant var + " .element1 span").text(message);
}
// If above 0.008, round 'message' to 3 decimal points, and increase 'var1'.
// This range does not involve interacting with another button, it increments var1
// to prevent clicking the wrong line on the .element2 button.
else if(message > 0.008){
message = message.toFixed(3);
function var1Count (){
var1++;
}
var1Count();
$("#" + irrelevant var + " .element1 span").text(message);
}
function testCount (){
count++;
}
testCount();
// Restarting the process on the next item in the list using the variable count.
$(".myButton").click (something);
document.getElementsByClassName("myButton")[count].click();