简体   繁体   中英

script - click button every x seconds

I want a script that clicks on this button every 3 seconds

<input type="submit" name="Submit" value="Puxar Alavanca">

It dsnt have any class, can you guys help me?

you can use jQuery to trigger a click event'

setInterval(function(){
   $( "input" ).trigger("click");
},random(3000,4000));

function random(min,max){
   return min + (max - min) * Math.random()
}

Try using setInterval() method . document.querySelector() selects your element,the setInterval() clicks button on every 3 seconds and the click event is handled by a function .

 var btn = document.querySelector("[name='Submit']"); //console.log(btn); setInterval(function(){ btn.click(); },3000); //Handling of click event btn.onclick=function(){ console.log('clicked'); } 
 <input type="submit" name="Submit" value="Puxar Alavanca"> 

The setInterval() method calls a function or evaluates an expression at specified intervals (in milliseconds).

This should do it

    <input type="submit" name="Submit" id="but" value="Puxar Alavanca">

    var but = document.querySelector("[name='Submit']");
    setInterval(function () {but.click();},3000);

You can visit this link to know more https://www.w3schools.com/js/js_timing.asp

Since you don't have a class name and such, you are going to want to make sure you only click the button you want when doing this.

With my version, it allows for gathering all the tags of type ' input ' and clicking the one you requested only - every 3-4 ( this number is randomly generated, sometimes 3 sometimes 4 ) seconds.

Please see below:

 // Random time between 3 and 4 seconds. var randomTime = (Math.floor(Math.random() * (4 - 3 + 1)) + 3) * 1000; setInterval(function() { var tags = document.getElementsByTagName("input"); for (var i = 0; i < tags.length; i++) { if (tags[i].value == "Puxar Alavanca") { tags[i].click(); // Below alert should be removed, just for testing/showing purposes. alert("Button clicked after " + randomTime + "ms."); } } }, randomTime); 
 <input type="submit" name="Submit" value="Puxar Alavanca"> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM