0

I have a situation whereby i need to delay/stall the code a little before the next line of code will be executed.

    elementslist1[Math.floor(Math.random() * 11)].click()
    elementslist2[Math.floor(Math.random() * 11)].click();
    elementslist3[Math.floor(Math.random() * 11)].click();
    elementslist4[Math.floor(Math.random() * 11)].click();
    elementslist5[Math.floor(Math.random() * 11)].click();
    elementslist6[Math.floor(Math.random() * 11)].click();
    elementslist7[Math.floor(Math.random() * 11)].click();
    elementslist8[Math.floor(Math.random() * 11)].click();

    var input = document.getElementById('inputelement');

I need the code to delay for sometime after elementslist8[Math.floor(Math.random() * 11)].click(); has executed.Then after this delay it can now proceed to the next line which is

var input = document.getElementById('inputElement');

Ivaka Zakuri
  • 57
  • 10

2 Answers2

0

So use a queue with a timeout

var elems = [elementslist1, elementslist2, elementslist3, elementslist4]

function clickNext() {
  // grab first item from queue
  elems.shift()[Math.floor(Math.random() * 11)].click()
  // if array still has items
  if (elems.length) {
    window.setTimeout(clickNext, 2000)
  }
}

// start it
clickNext();
epascarello
  • 204,599
  • 20
  • 195
  • 236
0

That's not possible in JavaScript.

You can use setTimeout though, it will be more complex than what you're asking though.