PDA

View Full Version : Waiting in functions



INVAL1D
Jan 3rd, 2015, 04:10 PM
I made a wait time (3 seconds) in a function I made, yet when I call the function, it spam throws "iterating"
and crashes Chrome.


function Check() {
setTimeout(Check,3000);{
location.reload();
document.getElementsByClassName("Item")[0].click();
}
}
while (document.getElementById('ctl00_cphRoblox_rbxUserPane_lUserOnlineStatus').getAttribute("class") === "UserOfflineMessage" || document.getElementById('ctl00_cphRoblox_rbxUserPane_lUserOnlineStatus').getAttribute("class") === "UserOnlineMessage") {
Check();
}


What is wrong with this code?

Arbitrator
Jan 3rd, 2015, 09:32 PM
What is wrong with this code?Unless those class attribute values are set only momentarily by the page's code, your code will either do nothing or run infinitely.

You also reload the page before you execute the click method, so there's no guarantee that a click will occur since the page may reload too fast.

If the class attribute values change automatically (without a page reload), you may want to use a mutation observer to monitor them. Otherwise, it looks like you'll be wanting to reload the page: check for whatever it is you want to click, click if the check passes, then reload the page.

Old Pedant
Jan 3rd, 2015, 10:55 PM
Unless those class attribute values are set only momentarily by the page's code, your code will either do nothing or run infinitely.

Umm...actually, that while loop WILL run infinitely, or at least as long as the page is in memory. Whether or not it continues to call Check( ) just depends on that class attribute being set to EITHER ONE of those values. But as soon as it matches either value, the Check() function will be called. And that means *IMMEDIATELY* the page will be reloaded. Which stops the loop. I'm not clear where the "momentarily" part comes in.

That 3 second timeout will never do anything. Because the page is reloaded long long long before the 3 seconds are up.


You also reload the page before you execute the click method, so there's no guarantee that a click will occur since the page may reload too fast.
I am betting that he wants the document.getElementsByClassName("Item")[0].click(); to execute *AFTER* the page is reloaded, in the new version of the page. And of course that will NEVER happen.

Anyway, I for one don't pretend to understand what this code is supposed to be doing.

Old Pedant
Jan 3rd, 2015, 11:02 PM
INVALID: It might help to point out that you have an extra set of braces {...} that are doing absolutely nothing.

That is, you could (and probably should) have written you Check() function thus:

function Check() {
setTimeout(Check,3000);
location.reload();
document.getElementsByClassName("Item")[0].click();
}

But understand that all your timeout is doing is saying "call this same function again in 3 seconds". And then the code CONTINUES. So it sets up the timeout, yes, but then it *IMMEDIATELY* does the location.reload(). And it doesn't make much sense to do that call to click(), because although it might click the button (or whatever it is), the reload( ) is going to wipe the page out of memory and reload it back to the original state. (Note: In fact, a browser could choose to not even attempt to execute the click( ), knowing that the reload( ) essentially terminates the current page's existence. I don't know if any browser does this, but I can't see why they couldn't.)

If you are trying to get the reload( ) delayed for 3 seconds, you would need to put it in a SEPARATE function.

But in any case your infinite while loop is a horrible idea. It *WILL* eat up the entire CPU time allocated to JavaScript and no other code will be able to run.

Arbitrator
Jan 4th, 2015, 05:25 AM
Umm...actually, that while loop WILL run infinitely, or at least as long as the page is in memory. Whether or not it continues to call Check( ) just depends on that class attribute being set to EITHER ONE of those values. But as soon as it matches either value, the Check() function will be called. And that means *IMMEDIATELY* the page will be reloaded. Which stops the loop. I'm not clear where the "momentarily" part comes in.Hmm... I guess you're right. I figured an asynchronous change to the class attribute might terminate the while loop, but browsers still get locked up by the loop.

I used the below code to test that. It'll lock up Firefox 34 until a dialog appears with a "Stop script" button, while Chrome 39 displays a "Something went wrong..." fallback page after a certain period of time and Internet Explorer 11 tries and gives up on (re-)loading the page after clicking "Allow blocked content". This is true even though there's a mutation observer that should make the while loop condition false mid-loop.


<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>HTML Demo</title>
<style>
html {
background-color: white;
color: black;
}
p:not(.good) {
color: red;
}
p.good {
color: green;
}
</style>
</head>
<body>
<p>This is a paragraph. It should have green text.</p>
<script>
(function () {
"use strict";
var paragraph = document.getElementsByTagName("p").item(0);
var mutationObserver = new MutationObserver(normalizeParagraphState);
var mutationObserverOptions = {
attributes: true,
attributeFilter: ["class"]
};
function normalizeParagraphState() {
mutationObserver.disconnect();
paragraph.setAttribute("class", "good");
mutationObserver.observe(paragraph, mutationObserverOptions);
}
mutationObserver.observe(paragraph, mutationObserverOptions);
paragraph.setAttribute("class", "bad");
while (paragraph.hasAttribute("class") && paragraph.getAttribute("class") === "bad") {
var additionalParagraph = document.createElement("p");
additionalParagraph.textContent = "This is an additional paragraph.";
document.body.appendChild(additionalParagraph);
}
})();
</script>
</body>
</html>


I am betting that he wants the document.getElementsByClassName("Item")[0].click(); to execute *AFTER* the page is reloaded, in the new version of the page. And of course that will NEVER happen.That seemed like a possibility, but since I wasn't sure, I didn't mention it.


Anyway, I for one don't pretend to understand what this code is supposed to be doing.My best guess is that the OP is trying to automate one of those tedious Web-based games to play itself. If you look up "Roblox" (found in the ID), that would seem to be accurate.

rnd me
Jan 4th, 2015, 01:20 PM
surely no game worth cheating is made in .NET webforms...