Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

223
Views
How to set delay in random function?

I write a short code for browser automation testing on my website, browse random part of the website and just basic navigation, i want a small delay between the navigation. But setTimeout(); aint gonna cut it because when browse(); it skipped all setTimeout(); in sequence and i cant get it to work.

function findError() {
  if (window.find('Error')) {
    window.location.reload();
  }
}
function browse(url) {
  window.location.assign(url);
}
function click (x,y) {
  document.elementFromPoint(x,y).click();
}
var funcs = [
  func1,
  func2
]
function execute() {
  var i = Math.floor(Math.random() * funcs.length)
  funcs.splice(i-1, 1)[0]()
}
for ( var i = 0; i < 30; i++ ) {
  function func1() {
    browse('https://google.com')
    setTimeout(function() {findError();}, 10000);
    setTimeout(function() {click(420,540);}, 20000);
  }
  function func2() {
    browse('https://netflix.com');
    setTimeout(function() {findError();}, 10000);
    setTimeout(function() {click(1210,310);}, 20000);
  }
  setTimeout(function() {execute();}, 40000);
}

I have tried to use this delay but it only work on top of body modules, not in another function

function sleep(ms) {
  return new Promise(resolve => setTimeout(resolve, ms));
}
await sleep (ms);

Do you have any idea ? Please help me

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!