TaoJS TaoJS - 25 days ago 11
jQuery Question

How to Delay js function after click

I want to add a delay after clicking a button to get some data.
The script is run inside the browser console.

$(pages()) get's the pagination buttons.

let calls = [];
for (let i = 1; i <= callPagesCount; i++) {
$(pages()).find('a')[i].click()

setTimeout(() => {}, 200)

// Call the below, 200 ms later
callsNode().map((i, el) => {
calls.push({
call_condition: $(el).find('div > span').text().trim(),
date: $(el).find('div').text().trim().match(/\[(.+)\]/i),
})
})
}

Answer Source

You can use $.map(), .delay(), .queue(), .promise()

$(() => {
  $("a").on("click", e => {
    e.preventDefault();
    console.log("clicked")
  });

  $({}).queue("pages"
  , $.map($("a") /* Array.from({length:callPagesCount}, (_, i) => i + 1) */ 
  , (el, i) =>
    (next) => {
      el.click();
      /*
      $(pages()).find('a')[i].click()
      */
      $({}).delay(1000 /* 200 */ , "delay")
        .queue(_next => {
          /*
          callsNode().map((i, el) => {
            calls.push({
              call_condition: $(el).find('div > span').text().trim(),
              date: $(el).find('div').text().trim().match(/\[(.+)\]/i),
            })
          })
          */
          _next();
        }).dequeue("delay")
        .promise("delay").then(next)
    }
  )).dequeue("pages");
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js">
</script>
<a href="">a</a>
<a href="">b</a>