0

I am new to coding with js, and have tried many different ways to loop this code, as well as asking a friend of mine who is a bit more proficient than I am, and he was incorrect as well. I looked up how to use loops in js as well, and I seem to be stumped, so if you could also give me a basic explanation as to how loops in js work, that'd be great!

ORIGINAL CODE

function partA() {
var classes1 = document.getElementsByClassName('_jvpff _k2yal _csba8 _i46jh _nv5lf'); // finds follow button
var Rate1 = classes1[0];Rate1.click(); // clicks button1

}

setTimeout(partB, 20000); // begins func. B about 17 seconds after func a has been completed
function partB() {
var classes2 = document.getElementsByClassName('_de018 coreSpriteRightPaginationArrow'); // finds “next” arrow
var Rate2 = classes2[0];Rate2.click(); // clicks next arrow

}

partA(); // runs functions

The original code itself works fine, but it never seems to work with any loops I use.

Most Recent Loop Attempt - Note: failed, obviously

function partA() {
var classes1 = document.getElementsByClassName('_jvpff _k2yal _csba8 _i46jh _nv5lf'); // finds follow button
var Rate1 = classes1[0];Rate1.click(); // clicks button1

}

setTimeout(partB, 20000); // begins func. B about 17 seconds after func a has been completed
function partB() {
var classes2 = document.getElementsByClassName('_de018 coreSpriteRightPaginationArrow'); // finds “next” arrow
var Rate2 = classes2[0];Rate2.click(); // clicks next arrow

}

partA(); // runs functions

for (i = 0; i < 30; i++) {
text += “The number is ” + i + “<br>”;

}

Thank you in advance! - Michael

Any tips to just generally improve the code would also be appreciated.

Michael B
  • 23
  • 1
  • 5
  • Are you trying to have the functions call each other? What are you trying to achieve with your code? – Beamer180 Mar 21 '16 at 03:51
  • I was going to ask exactly what @Beamer180 asked above. An important part of asking questions on SO is to ask them clearly. – shashanka n Mar 21 '16 at 03:52
  • Also, a couple of things: you are not using `text` anywhere, and you are using the wrong quotes (use `" "` not `“ ”`) – JCOC611 Mar 21 '16 at 03:54
  • @shashankan I'm trying to loop part A. part A then serves to start part B, but that doesn't really need changing. I need to figure out how to loop A. – Michael B Mar 21 '16 at 03:55
  • @Beamer180 I'm trying to loop part A. part A then serves to start part B, but that doesn't really need changing. I need to figure out how to loop A. – Michael B Mar 21 '16 at 03:55
  • @JCOC611 Noted. Thanks, I also changed the quotations. – Michael B Mar 21 '16 at 03:59
  • @MichaelB If you're trying to loop part A, then why is the function call outside the loop? – shashanka n Mar 21 '16 at 04:00
  • @shashankan Ah. Stupid mistake on my part. But what do I define " text " as? Sorry for my ignorance, heh. Teenager learning to code. – Michael B Mar 21 '16 at 04:04
  • Seems like you're not really asking about how loops work, but more about how Javascript interacts with a HTML document. Start here: http://stackoverflow.com/questions/1533568/what-is-the-correct-way-to-write-html-using-javascript – Tibrogargan Mar 21 '16 at 04:11
  • @Tibrogargan Thanks, I read it, and it helped somewhat, but I honestly need specific information on loops at the moment. – Michael B Mar 21 '16 at 04:28
  • Maybe rephrase your question then. It's really not clear what you're trying to do. (Simulate pressing a button after 20 seconds ... but why do you need a loop to do that?) – Tibrogargan Mar 21 '16 at 04:30
  • @Tibrogargan I am doing two things in this script, #1, I am clicking a button on a blog I am working on. After that, The script is supposed to click a second button to go to the next blog post, so I can test out my blog long-term, free-hands, theoretically while writing something to go along with my post. I want function partA to loop, and partA itself causes the rest to loop. – Michael B Mar 21 '16 at 04:38

1 Answers1

0

Still can't work out exactly what you're after (looks like: trying to automate some repetitive task in some page for which you don't control the source)

<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
<title>JS Loop Example?</title>
<script>
function foo() {
    var div = $("#x")[0];
    div.innerHTML = "foo was clicked";
    for (i = 0; i < 5; i++) {
        div.innerHTML += "<br />!";
    }
    setTimeout(function(){ $('.barButton').click() }, 3000)
}
function bar() {
    var div = $("#x")[0];
    while (div.firstChild) {
        div.removeChild(div.firstChild);
    }
    var wibble = document.createTextNode('bar was clicked');
    div.appendChild(wibble);
    for (i = 0; i < 5; i++) {
        div.appendChild(document.createElement('br'));
        wibble = document.createTextNode('?');
        div.appendChild(wibble);
    }
    setTimeout(function(){ $('.fooButton').click() }, 3000)
}
</script>
</head>
<body onload='setTimeout(foo, 3000)'>
<script>
// Up until the close of the body, I can just write into the document.
document.write('<div id="x" class="stuffGoesHere">');
document.write('some random text<br />');
for (i = 0; i < 5; i++) {
    document.write('#<br />');
}
document.write('</div>');
document.write('<input type="button" class="fooButton" value="foo" onClick="foo()" />');
document.write('<input type="button" class="barButton" value="bar" onClick="bar()" />');
</script>
</body>
</html>
Tibrogargan
  • 4,508
  • 3
  • 19
  • 38