+ 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

2nd Jun 2019, 5:22 PM
Oma Falk
Oma Falk - avatar
4 Answers
+ 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));
2nd Jun 2019, 5:30 PM
Schindlabua - avatar
+ 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); } }
2nd Jun 2019, 11:40 PM
InvBoy [ :: FEDE :: ]
InvBoy [ :: FEDE :: ] - avatar
+ 3
Schindlabua yeah thats magic... Got it more or less.
2nd Jun 2019, 5:52 PM
Oma Falk
Oma Falk - avatar
+ 1
setInterval($('yourbtn').click(), 500)
3rd Jun 2019, 4:04 PM
Roj Serbest