Trigger mouse click and random intervals

I am trying to automate a task in which I have to constantly left-click on the hand icon. I can do this for a given time interval, for example 32 seconds and 756 ms, but I need to do this with a random timeframe. For example, if we can add in 2-3 seconds after each left-click. Can someone tell me how to make the click interval random? I am using Chrome.

setInterval(function(){ 
    $( "[id^='hand_'].handIcon").trigger('click');
}, 32756);
+4
source share
4 answers

Even if you use Math.random in setInterval, it will only register once with a specific random value.

There are two options:

  • , ( clearInterval )
  • x ms , , .

Eg. :

setInterval(function() { 
  if (Math.random() > 0.8)
    $( "[id^='hand_'].handIcon").trigger('click');
}, 200);

200 , 0,8 , , 250 , . , .

, (rs):

let action = () => $( "[id^='hand_'].handIcon").trigger('click');
let old, register = () => {
  if (old) clearInterval(old);
  old = setInterval(() => {
    action();
    register(); // re-register
  }, 32756 + 3000*Math.random() - 1500)
};
register();
+2

, setTimeout. 32000 35000 (32-35 ).

var clickHand = function() {
  $("[id^='hand_'].handIcon").trigger('click');
  setTimeout(clickHand, (Math.random() * 3000) + 32000);
}

clickHand();
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
Hide result
+2

Timeout

var timeout = function(callback) {
   var time = Math.floor(Math.random()*32756);
   return setTimeout(function(){
      callback();
      timeout(callback);
   }, time)
};

timeout(function(){
   $( "[id^='hand_'].handIcon").trigger('click');
})
0

setIntervalUse instead setTimeout. Then it will only start once, and you can set a new (random) timeout after this period. You can wrap it in a nice function that can be called just like setIntervalit setTimeout, but with a range instead of a single value.

// Generic function to set this interval
function setRandomInterval(f, min, max) {
  setTimeout(function() {
    f();
    setRandomInterval(f, min, max) 
  }, min + Math.random() * (max - min));
};

// Calling it, specifying a min and a max timeout
setRandomInterval(function(){

  console.log(new Date()); // For demo
  //$( "[id^='hand_'].handIcon").trigger('click');

}, 200, 3000);
Run codeHide result
0
source

Source: https://habr.com/ru/post/1665650/


All Articles