+ 10
How to click Button each half second automatically
Have a look and function solution(). I hoped to click the buttons in the array. It does the job but does not render or wait. edit: solution does it now. renamed my problem function to solution2() https://code.sololearn.com/W8fO9n0IbgDh/?ref=app
4 ответов
+ 10
Your solution doesn't wait because setTimeout unfortunately doesn't block until the timeout is over, but we can beat setTimeout into shape.
Not sure how comfortable you are with promises and async/await, but I would mark the function as async and wait 500ms in between each step like this:
async function solution(){
sol = ...
for(let step of sol){
$(step).click();
await wait(500);
}
}
The magic is in the wait function, I use it in every program:
const wait = timeout => new Promise(resolve => setTimeout(resolve, timeout));
+ 10
Another solution:
function solution2(){
sol = [left,up,right,down,left,up,down,down,right,left,up,right,left,down]
for(let i in sol) {
setTimeout(
function() {$(sol[i]).click()}
, 500*i);
}
}
+ 3
Schindlabua yeah thats magic...
Got it more or less.
+ 1
setInterval($('yourbtn').click(), 500)