Connection request to LinkedIn profiles on search page I implemented a script for a LinkedIn user to connect with all profiles on a LinkedIn search page and move to the next one automatically.

As I am a new javaScript programmer, could someone look at my code and tell me how to write better code?

// Every 1 second, click all the connect buttons on the result page 
window.setInterval(function () {
    $("a:contains('Connect')").each(function (index, a) {
        $(this).trigger('click');
    });

    //Scroll down to the button of the page
    window.scrollTo(0, document.body.scrollHeight);
}, 1000);

//Click Next after every 5 seconds
window.setInterval(function () {
    $("a:contains('Next &gt;')")[0].click();
}, 5000);


Steps to run and test are all given here.
It doesn't sound like a great idea to run something infinitely.
Especially, doing $("a:contains('Next &gt;')")[0].click() periodically is flawed, because when there is no more Next links, the dom query will return empty result, the [0] index of which is undefined, and the .click() will throw a TypeError.

I suggest to make it possible to stop the periodic calls, especially the second one. See this related answer for a technique that you can use.



In this code, the parameters index and a of the anonymous function are never used:


$("a:contains('Connect')").each(function (index, a) {
    $(this).trigger('click');
});



You could omit them, using simply function () { in the function declaration.



Instead of using anonymous functions in the setInterval calls,
I suggest to use named functions, something like this:

var clickConnect = function() { ... };

var clickNext = function() { ... };

window.setInterval(clickConnect, 1000);
window.setInterval(clickNext, 5000);


When written this way, the window.setInterval calls are easier to read,
When using anonymous functions,
it can be hard to search for the delay parameter,
as you have to scan the code to see where the function ends and the delay parameter begins.