admin管理员组

文章数量:1415099

im trying to make a script that picks 1 random class name out of 3 every 1 minute and clicks the button with the chosen class so far i created the script that clicks on the button:

setTimeout(function () {
    $(".btn-danger").trigger("click");
}, 100);

The problem is if i put it in a while(true) the site stuck and then the browser crashes.

Also im have no idea how to make it to chose random class so i putted in one of them.

Will be glad to get some help here :D

im trying to make a script that picks 1 random class name out of 3 every 1 minute and clicks the button with the chosen class so far i created the script that clicks on the button:

setTimeout(function () {
    $(".btn-danger").trigger("click");
}, 100);

The problem is if i put it in a while(true) the site stuck and then the browser crashes.

Also im have no idea how to make it to chose random class so i putted in one of them.

Will be glad to get some help here :D

Share Improve this question edited Dec 26, 2020 at 19:07 Tiago Peres 15.7k22 gold badges102 silver badges167 bronze badges asked Mar 12, 2016 at 18:50 user23634623user23634623 1611 gold badge5 silver badges11 bronze badges 1
  • You need a reset timer event - google it is a good start - have a look at this for example stackoverflow./questions/18270009/reset-timer-of-setinterval - @jfriend00 is exactly correct – StudioTime Commented Mar 12, 2016 at 18:54
Add a ment  | 

3 Answers 3

Reset to default 3

Check out setInterval() to run something over and over. You can generate a random index from 0 to 2 with Math.floor(Math.random() * 3).

For example, you could select a random class name like this:

var classes = ["classA", "classB", "classC"];

function selectRandomArrayElement(array) {
    return array[Math.floor(Math.random() * array.length)];
}

var rand = selectRandomArrayElement(classes);

So, putting it all together:

var classes = ["classA", "classB", "classC"];

function selectRandomArrayElement(array) {
    return array[Math.floor(Math.random() * array.length)];
}

// click on a the object with a random class name every minute
setInterval(function() {
    var rand = selectRandomArrayElement(classes);
    $("." + rand).trigger("click");
}, 1000*60);

In Javascript, you can't use while(true) long duration loops like this block the rest of the browser from processing events and thus your the click events you trigger are never processed. Instead, you use timers to do something repeatedly.

You're looking for setInterval instead of setTimeout, which will invoke the callback indefinitely at the interval you specify.

setInterval(function () {
  $(".btn-danger").trigger("click");
}, 60000);

Should work for you. while(true) crashes your browser because you're creating an infinite loop that blocks any other code from executing.

setTimeout uses milliseconds and only occurs once. Use setInterval with one second * 60 to get a minute.

var minute = 1000 * 60;
setInterval(function() {   $(".btn-danger").trigger("click");
  }, minute);

本文标签: javascriptRun a script in chrome every 1 minuteStack Overflow