Tips for incorporating a pause or delay into if-else statements

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();

Answer №1

When it comes to automating tasks, using tools like selenium can be beneficial due to its automatic synchronization and the sleep() method.

If you're working with pure javascript, the setTimeout function is what you need.

Without a more detailed example of your code, it's not clear why you require this timeout in the first place. Providing a complete snippet would help us provide more targeted assistance.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Establishing the initial value for an input file form

Similar Question: Dynamically set value of a file input I'm currently working with an HTML form that includes a file input field, and I'm attempting to set the initial value for the file path in the form. I've tried changing the "value" ...

Is it possible that the background color won't change on the second click?

Initially, my first click worked fine and successfully changed the background color. However, as soon as I added a second condition, it stopped working. var showBox = $('.show'); showBox.click(function(){ if (parseInt($(this).attr('v ...

Concluding the use of angular-bootstrap-datetimepicker when selecting a date

I am currently utilizing the UI Bootstrap drop-down component to display the angular-bootstrap-datetimepicker calendar upon clicking. Although it works well for the most part, I have encountered an issue where the calendar block does not close when clicked ...

The for loop unexpectedly interrupts a different array

Hey there, I've been working with puppeteer and came across an issue with the following code snippet: console.log(dealsId[i]); for (var i = 0; i < sizes.length; i++) { var refIdClasses = await sizes[i].$eval('input', a => a.getAtt ...

I wish for the value of one input field to always mirror the value of another input field

There is a checkbox available for selecting the billing address to be the same as the mailing address. If the checkbox is checked, both values will remain the same, even if one of them is changed. Currently, I have successfully achieved copying the mailing ...

How can I extract a portion of an array of strings in C to use as an argv parameter in execve()?

Currently, my code is pretty chaotic and messy. Because of this, I believe it would be more effective to explain what I'm trying to achieve through description rather than looking at the code itself. For a homework assignment, I am developing a shell ...

Saving similar and dissimilar information in a database

I have been working on implementing a Like and Unlike feature for users to interact with products. Following this tutorial at , I've completed the necessary steps. However, I'm facing an issue where the likes are not being stored in the database ...

Can you recommend any open source projects with exceptionally well-written Jasmine or Jasmine-Jquery tests?

Currently, I am in the process of learning how to test a new jquery plugin that I plan to develop. I'm curious if there are any notable Github projects like Jasmine or Jasmine-jquery with impressively crafted Jasmine tests that I could explore for in ...

Leveraging jQuery in Content Scripts for Chrome Extensions

I am currently working on developing a Chrome extension that will prompt a small input whenever a user highlights text on a webpage (similar to Medium's feature that allows you to tweet highlighted text). While I am making progress, I believe using j ...

Adjust the cursor in a contenteditable division on Chrome or Webkit

Is there a way to set the caret position in a contenteditable div layer? After trying different methods and doing some research online, I finally found a solution that works in firefox: function set(element,position){ element.focus(); var range= w ...

Next.js encountered an issue: React.Children.only function was expecting to receive only one React element child, but received more

I have created a component named Nav located in the components directory. Here is the code for it: import Link from 'next/link'; const Nav = () => { return( <div> <Link href="/"> <a> Home </a> ...

In an attempt to evaluate the checkbox list values against those in an array list using C# and ASP.NET

We are in the process of developing a student information system that includes a checkbox list containing all the modules for the course enrolled by a selected student. These module values are stored in the database. Our goal is to automatically select/tic ...

ExpressJS subclasses are failing to inherit attributes and properties in callback functions

Currently, I am utilizing ExpressJS 4.16 on Node v8.9.4 and have established a base controller to manage default routes with the following example: class BaseController { constructor(name, app, router, data) { this._name = name; this._app = app; ...

Top method for passing props from child to parent React component

I am facing a challenge with passing data from child to parent components in React. I have an array that I need to save in my database, and I have set up a Parent component (AuthenticationPage.js) and a Child component (SignupForm.js) for this purpose. The ...

What is the best way to execute 2 statements concurrently in Angular 7?

My goal is to add a key rating inside the listing object. However, I am facing an issue where the rating key is not displaying on the console. I suspect that it might be due to the asynchronous nature of the call. Can someone help me identify what mistak ...

Clicking will cause my background content to blur

Is there a way to implement a menu button that, when clicked, reveals a menu with blurred content in the background? And when clicked again, the content returns to normal? Here's the current HTML structure: <div class="menu"> <div class=" ...

Protect database entries from cross-site scripting attacks

I have a project in progress where I am developing an application that extracts text from tweets, saves it to the database, and then presents it on the browser. I am currently concerned about potential security risks if the text contains PHP or HTML tags. ...

Is it possible for jquery JSON AJAX to block all users?

On my website, I use AJAX to load an RSS feed on the client side when the page loads. If a user repeatedly presses F5, could the owner of the RSS feed ban my entire website instead of just that one user? This would prevent others from loading the RSS feed ...

The radio button allows for the selection of multiple items, rather than just one at a time

https://i.sstatic.net/9MEzS.png I have implemented a radio input as shown below <input type="radio" id={el.name} className="form-check-input" name={el.name} data-count={el.name} value={el.id} onChange={this.select_role.bind(this)} style={{margin: "4px ...

Guide to using VueJS for sending a POST request with a JSON array containing data and uploading a File or Picture for each element in the array

I am currently facing a challenge with Axios where I need to send formData from a dynamic Form to PHP. Users should be able to add multiple "persons" along with their respective data and pictures. Below is the format of the data: data: { person:{ ...