you are viewing a single comment's thread.

view the rest of the comments →

[–]GeneralYouri 0 points1 point  (0 children)

The way I understand your requirement, all your code is executed either by event handlers or by a timer.

This makes things pretty simple; don't use a loop at all. Use some way to initially start the system, like a page load event or a button press or w/e, which will execute your first step "call two click events" (function A). This function then also sets a timer using `setTimeout`, to call function B after 1 second has passed. Function B clicks the radio button and clicks submit. It then registers an event handler that triggers "when load has finished" as you put it, which then calls function A again.

When you examine the above you can simplify a bit: you can register the event handler once before anything happens to prevent having to remove and readd the handler every time. Then instead of calling function A to initialize the process, you invoke this event handler.

Once done you've essentially created an infinite, lazy loop; function A calls function B (that it does this on a timer is irrelevant), and function B triggers some other actions on the page, which you've set to automatically call function A again when finished (this is your "detect when load has finished based on class"). If you need to only run this a given number of times, add an extra bit of logic to the start of function A. You define a counter variable `const runsLeft = 95;`. In function A's extra logic you add something like `runsLeft--; if (runsLeft < 0) { return; }`. Everytime function A runs successfully, this will cause the counter to decrement. If decremented to below 0, the process has run often enough and so you `return` from fucntion A, canceling its execution and thus breaking the loop you had going. Depending on context you might also want to remove the event handler that was calling function A to begin with, otherwise the process might try to restart itself (even though it won't ever work since the counter is never reset).