触发鼠标点击和随机间隔 [英] Trigger mouse click and random intervals
问题描述
我正在尝试自动化一项任务,而我必须不断地在手形图标上单击鼠标左键.我能够在设定的时间范围内执行此操作,例如32秒和756毫秒,但是我需要在随机时间范围内执行此操作.例如,如果我们可以在每次单击鼠标左键后增加2-3秒.谁能指导我如何使点击间隔随机化?我正在使用Chrome.
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);
推荐答案
即使您在setInterval上使用Math.random,它也只会使用该特定随机值注册一次.
Even if you use a Math.random at the setInterval, it will only register once, with that specific random value.
有两种选择:
- 运行一次,然后使用新的随机时间范围重新注册(使用clearInterval删除旧的时间范围)
- 每x ms运行一次,并添加随机检查以查看是否运行
例如.第二种情况:
setInterval(function() {
if (Math.random() > 0.8)
$( "[id^='hand_'].handIcon").trigger('click');
}, 200);
这将每200毫秒运行一次,但仅运行0.8次,例如,平均每250毫秒运行一次,但是是随机的.您当然可以调整数字.
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.
第一个示例,因为我今天(rs)确实受到了启发:
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();
这篇关于触发鼠标点击和随机间隔的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!