Asynchronous running required?

  Kiến thức lập trình

I’m trying to automate a task using JavaScript by running it in the Google Chrome console (not sure if that matters, I’m pretty new). I have already done most of the script, but more problems keep popping up the moment I’m done with the “last bit” of the script. Essentially, I have a list of IDs to search up on a company webpage, numbering around 20k IDs, and can only search 20 IDs at a time. That portion is fine, but the problem is that I would need to wait for the webpage to finish loading, (which I think is synchronous) before moving on to the next 20 IDs, but my webpage would freeze using whatever methods I’ve tried. I would like to ask if an asynchronous solution is required if I would like to wait for the page to finish loading anyway?

What steps should I take to get to my solution? Thanks!

When my webpage loads, it would use an element with id ‘cover’ and change it’s display from ‘none’ to ‘block’. So far I’ve tried

  1. For Loop
    for (var s = 0; s < 1;) {
        tool20 = splitSearch(s); //tool20 gets the results of the 20 IDs
        for (var t = 0; t < tool20.length; t++){
            allTools.push(tool20[t]);
        }
        document.getElementById('bad').value = [];
        if(document.getElementById('cover').style.display === 'block'){
            inc = 0;
            }
        if(document.getElementById('cover').style.display === 'none'){
            inc = 1;  
            }
        s += inc;
}
  1. While loop
var i = 0;
search(); //function to search
//assuming the moment my script hits the search button the display is already on 'block'
While(i == 0){
    if(document.getElementById('cover').style.display === 'none'){
    inc = 1;
    //Remaining code to run after loading
    }
}

Both of these methods will end up freezing my page
(sorry for formatting I’m not sure how to use stack overflow)

LEAVE A COMMENT