admin管理员组文章数量:1419219
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);
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);
Share
Improve this question
edited Jan 3, 2017 at 18:48
Heretic Monkey
12.1k7 gold badges61 silver badges131 bronze badges
asked Jan 3, 2017 at 18:38
SubodhSubodh
713 silver badges10 bronze badges
4 Answers
Reset to default 3Use 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./ajax/libs/jquery/2.1.1/jquery.min.js"></script>
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();
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');
})
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);
本文标签: javascriptTrigger mouse click and random intervalsStack Overflow
版权声明:本文标题:javascript - Trigger mouse click and random intervals - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745293663a2651948.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论