Trigger mouse click and random intervals

佐手、 提交于 2021-02-20 19:05:11

问题


I am trying to automate a task where I have to continuously do left mouse click on an hand icon. I am able to do that on a set time frame, for example 32 sec and 756 ms but I need to do this with a random timeframe. For example if we can add 2-3 seconds after each left mouse click. Can anyone guide me how to make the click interval random? I am using Chrome.

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

回答1:


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

There are two options:

  • run once and then re-register with a new random timeframe (use clearInterval to remove the old ones)
  • run every x ms and add a random check to see whether to run or not

Eg. of the second case:

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

This will run every 200ms, but only 0.8 of the times, e.g., on average every 250ms, but random. You can tweak the numbers, of course.

Example of the first one, because I'm really inspired today (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:


Use a recursive function that calls itself after a setTimeout. This will run infinitely, and each time at a random interval between 32000 and 35000 milliseconds (32 to 35 seconds).

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>



回答3:


Your answer is setTimeout as callback with random delay

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');
})



回答4:


Instead of setInterval use setTimeout. Then it will run only once, and you can set a new (random) timeout after that period. You can wrap it in a nice function which can be called in a similar fashion as setInterval and 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);


来源:https://stackoverflow.com/questions/41450212/trigger-mouse-click-and-random-intervals

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!