Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Running a jQuery function multiple times sequentially (for a Bookmarklet)

I've got the following jQuery code which I use in a Bookmarklet. It clicks on all the buttons on the page (with the class "Unfollow") one by one, with a random time between each one...

javascript: (function() {
    var unfollowButtons = $('button.Unfollow');
    var index = unfollowButtons.length - 1;
    unfollow();

    function unfollow() {
        if (index >= 0) {
            $(unfollowButtons[index--])
                .click();
            setTimeout(unfollow, Math.floor((Math.random() * 1000) + 500));
        }
    }
})();

I'd like to run the above function again twice once it has completed its cycle.

Just running the function again causes that to run in parallel with the first function call.

How do I run the unfollow() function 2 or 3 times without them all running in parallel?

like image 494
iagdotme Avatar asked Jun 21 '17 09:06

iagdotme


People also ask

How do I Schedule A Jax request twice?

Click #trips, load that code via ajax and click # schedule it calls my second ajax request, posts the data once. If I do the exact same steps a second time excluding the control F5 refresh, the # schedule ajax loops twice, and 3 times on a third repeat of the steps and so forth.

Are bookmarklets a good way to learn JavaScript?

Because bookmarklets are, by definition, extraneous, many of the guidelines for JavaScript—such as unobtrusiveness and graceful degradation—aren’t as sacred as they normally are. For the most part, though, a healthy understanding of best practices for traditional JavaScript and its frameworks will only help you:

What is the syntax for event methods in jQuery?

jQuery Syntax For Event Methods. In jQuery, most DOM events have an equivalent jQuery method. To assign a click event to all paragraphs on a page, you can do this: $("p").click();

What are bookmarklets and how do they work?

Because they run on JavaScript (a client-side programming language), bookmarklets (sometimes called “favelets”) are supported by all major browsers on all platforms, without any additional plug-ins or software needed. In most instances, the user can just drag the bookmarklet link to their toolbar, and that’s it! More after jump!


2 Answers

Try it this way (using ES6 Promises):

var runUnfollow = function() {
  return new Promise(function(resolve, reject){
    var index = unfollowButtons.length - 1;

    // fencepost for the loop
    var p = Promise.resolve();

    // we stop execution at `i == 0`
    for (var i = index; i >= 0; i--) {
      // run the promise
      // then set `p` as the next one
      p = p.then(unfollowTimeout.bind(null, i));
    }
    // make sure we run the last execution at `i == 0`.
    p.then(function(){
      resolve();
    })

    function unfollowTimeout(i){
      // return a promise to run `unfollow` and a `setTimeout`
      return new Promise(function(resolve,reject){
         unfollow(i);
         setTimeout(resolve, Math.floor((Math.random() * 1000) + 500));
      })
    }
    function unfollow(i) {
      $(unfollowButtons[i])
        .click();
    }
  })
}

// run three times synchronously
runUnfollow().then(runUnfollow).then(runUnfollow).then(function(){
  //finished
});

// another way to run three times synchronously
p = runUnfollow();
for(i=3; i > 0; i--){
  p = p.then(runUnfollow);
}
p.then(function(){
  //finished
});

// run in parallel
Promise.all([runUnfollow, runUnfollow, runUnfollow])
  .then(function(){
    //finished
  });

EDIT: Went back and read your question again, realized you were trying to run everything multiple times. I've edited to reflect that.

like image 86
Ezra Chu Avatar answered Nov 12 '22 07:11

Ezra Chu


Just reset index and restart after each button is clicked:

javascript: (function() {
    var unfollowButtons = $('button.Unfollow');
    var index = unfollowButtons.length - 1;
    var totalRuns = 3;
    unfollow();

    function unfollow() {
        if (index < 0 && totalRuns) {
            totalRuns--;
            unfollowButtons = $('button.Unfollow');
            index = unfollowButtons.length - 1;
        }

        if (index >= 0) {
            $(unfollowButtons[index--])
                .click();
            setTimeout(unfollow, Math.floor((Math.random() * 1000) + 500));
        }
    }
})();
like image 41
Leo Jiang Avatar answered Nov 12 '22 08:11

Leo Jiang